Chameleon

Chameleon Svn Source Tree

Root/branches/slice/i386/modules/NVRAM/NVRAM.c

1/*
2 * Idea and template by (c) 2009 Evan Lojewski. All rights reserved.
3 *
4 * NVRAM module by Slice 2010.
5 */
6
7#include "libsaio.h"
8#include "modules.h"
9#include "boot.h"
10#include "bootstruct.h"
11#include "pci.h"
12#include "efi.h"
13#include "smbios_patcher.h"
14
15#ifndef DEBUG_NVRAM
16#define DEBUG_NVRAM 0
17#endif
18
19#if DEBUG_NVRAM
20#define DBG(x...) verbose(x)
21#else
22#define DBG(x...) msglog(x)
23#endif
24
25/*static const char const FIRMWARE_REVISION_PROP[] = "firmware-revision";
26static const char const FIRMWARE_ABI_PROP[] = "firmware-abi";
27static const char const FIRMWARE_VENDOR_PROP[] = "firmware-vendor";
28static const char const FIRMWARE_ABI_32_PROP_VALUE[] = "EFI32";
29static const char const FIRMWARE_ABI_64_PROP_VALUE[] = "EFI64";
30static const char const SYSTEM_ID_PROP[] = "system-id";
31static const char const SYSTEM_SERIAL_PROP[] = "SystemSerialNumber";
32static const char const SYSTEM_TYPE_PROP[] = "system-type";
33static const char const MODEL_PROP[] = "Model";
34 */
35const char PLATFORM_UUID[] = "platform-uuid";
36//EFI_UINT32 const FIRMWARE_FEATURE_MASK = 0x000003FF;
37EFI_UINT32 const STATIC_ZERO = 0;
38
39#define kBL_GLOBAL_NVRAM_GUID "8BE4DF61-93CA-11D2-AA0D-00E098032B8C"
40
41// Check if a system supports CSM legacy mode
42#define kBL_APPLE_VENDOR_NVRAM_GUID "4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14"
43#define UUID_LEN16
44
45extern EFI_GUID* getSystemID();
46void NVRAM_hook(void* arg1, void* arg2, void* arg3, void* arg4);
47void NVRAM_start(void);
48int readNVRAM(void);
49
50typedef struct {
51char Name[32];
52char Value[512];
53} variables;
54variables* var;
55
56void NVRAM_hook(void* arg1, void* arg2, void* arg3, void* arg4)
57{
58charbootName[128];
59
60char*ffName;
61uint8_t *FirmwareFeatures;
62uint8_t *FirmwareFeaturesMask;
63char*ffmName;
64char*boName;
65char*bnName;
66EFI_GUID*ret = 0;
67uint16_tbootOptionNumber = 0;
68//msglog("NVRAM is not implemented yet\n");
69
70//return;
71
72msglog("NVRAM started\n");
73
74//Slice create /options node -> /fakenvram
75// I need to use fakenvram until I know what is happen
76//bool UseNVRAM = FALSE;
77bool ClearNVRAM = FALSE;
78const char* buff;
79int cnt;
80ClearNVRAM = getValueForKey(kClearNVRAM, &buff, &cnt, &bootInfo->bootConfig);
81if (!ClearNVRAM) {
82readNVRAM();
83}
84
85Node* optionsNode = DT__FindNode("/options", true);
86ffName = malloc(sizeof(PLATFORM_UUID)+1);
87strcpy(ffName, PLATFORM_UUID);
88ret = getSystemID();
89DT__AddProperty(optionsNode, ffName, UUID_LEN, (EFI_UINT32*) ret);
90
91// this information can be obtained from DMI Type 0
92SMBByte* p = (SMBByte*)FindFirstDmiTableOfType(0, 0x18);
93FirmwareFeatures = malloc(sizeof(SMBByte));
94//TODO - the bufferFF must be composed from bits p[0x12] and [0x13]. Datasheet needed
95*FirmwareFeatures = ((p[19] >> 1) & 1) //USB Legacy is supported
96| ((p[18] >> 14) & 2) //Boot from CD is supported
97| 0x14; //default for bless (GUID partition)
98
99sprintf(bootName, "%s:FirmwareFeatures", kBL_APPLE_VENDOR_NVRAM_GUID);
100ffName = malloc(sizeof(bootName)+1);
101strcpy(ffName, bootName);
102DT__AddProperty(optionsNode, ffName, sizeof(uint32_t), (char *)FirmwareFeatures); //legacy support
103
104sprintf(bootName, "%s:FirmwareFeaturesMask", kBL_APPLE_VENDOR_NVRAM_GUID);
105FirmwareFeaturesMask = malloc(sizeof(EFI_UINT32));
106*FirmwareFeaturesMask = (EFI_UINT32)0x3ff;
107ffmName = malloc(sizeof(bootName)+1);
108strcpy(ffmName, bootName);
109DT__AddProperty(optionsNode, ffmName, sizeof(uint32_t), (EFI_UINT32*)FirmwareFeaturesMask);
110
111//TODO - check, validate and fill by bvr structure.
112//here I am not sure what is BootOrder: node or property?
113//Node* bootNode = DT__AddChild(optionsNode, kBL_GLOBAL_NVRAM_GUID ":BootOrder");
114sprintf(bootName, "%s:BootOrder", kBL_GLOBAL_NVRAM_GUID);
115boName = malloc(sizeof(bootName)+1);
116strcpy(boName, bootName);
117DT__AddProperty(optionsNode, boName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
118
119sprintf(bootName, "%s:Boot%04hx", kBL_GLOBAL_NVRAM_GUID, bootOptionNumber);
120bnName = malloc(sizeof(bootName)+1);
121strcpy(bnName, bootName);
122DT__AddProperty(optionsNode, bnName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
123
124
125//can we add here boot-properties?
126//optionsNode = DT__FindNode("chosen", true);
127#if 1 //NOTYET
128int lbC = 0;
129while(((char*)&bootInfo->bootConfig)[lbC++]);
130if (lbC > sizeof(bootInfo->bootConfig)) lbC = sizeof(bootInfo->bootConfig);
131DT__AddProperty(optionsNode, "boot-args", lbC, (char*)&bootInfo->bootConfig);
132#endif
133//TODO - BootCamp emulation?
134/*
135 romNode = DT__FindNode("/rom", true);
136 DT__AddProperty(romNode, "fv-main-address"... //provided by AppleSMBIOS
137 DT__AddProperty(romNode, "fv-main-size"...
138 "IOEFIDevicePathType" -> "MediaFirmwareVolumeFilePath"
139 "Guid" -> "2B0585EB-D8B8-49A9-8B8C-E21B01AEF2B7"
140
141 */
142//end Slice
143}
144
145void NVRAM_start()
146{
147register_hook_callback("Kernel Start", &NVRAM_hook);
148}
149
150const char NVRAM_INF[] = "nvram.inf";
151int readNVRAM()
152{
153int fd, fsize;
154char* nvr = 0;
155if ((fd = open(NVRAM_INF, 0)) < 0) {
156return -1;
157}
158fsize = file_size(fd);
159if (!fsize) {
160close (fd);
161return -1;
162}
163if ((nvr = malloc(fsize)) == NULL) {
164verbose("[ERROR] alloc NVRAM memory failed\n");
165close (fd);
166return -1;
167}
168if (read (fd, nvr, fsize) != fsize) {
169verbose("[ERROR] read %s failed\n", NVRAM_INF);
170close (fd);
171return -1;
172}
173close (fd);
174if ((var = malloc(fsize)) == NULL) {
175verbose("[ERROR] alloc VAR memory failed\n");
176return -1;
177}
178int i = 0;
179bool skipGUID;
180while (*nvr) {
181int j = 0;
182skipGUID = false;
183while (*nvr != 9) {
184if (*nvr == 4) {
185skipGUID = true; //TODO this is GUID
186}
187var[i].Name[j++] = *nvr++;
188}
189nvr++; //skip \09
190var[i].Name[j] = 0; //end of c-string
191if (skipGUID) {
192//TODO this is GUID
193while (*nvr++ != 0x0A) {}
194i--;
195} else {
196
197j = 0;
198char c;
199while (*nvr != 0x0A) {
200c = *nvr++;
201if (c == 0x25) { //TODO this is hex
202int k1=*nvr++;
203if ((k1 > 0x30) && (k1 < 0x39)) {
204k1 = k1 - 0x30;
205}
206if ((k1 > 0x60) && (k1 < 0x69)) {
207k1 = k1 - 0x60 + 10;
208}
209int k2=*nvr++;
210if ((k2 > 0x30) && (k2 < 0x39)) {
211k2 = k2 - 0x30;
212}
213if ((k2 > 0x60) && (k2 < 0x69)) {
214k2 = k2 - 0x60 + 10;
215}
216c = (k1 << 4) + k2;
217break;
218}
219var[i].Value[j++] = c;
220}
221}
222i++;
223if (i > fsize) {
224break;
225}
226}
227
228return 0;
229}

Archive Download this file

Revision: 681