Chameleon

Chameleon Svn Source Tree

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

1/*
2 * 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_getters.h"
14#include "xml.h"
15
16#ifndef DEBUG_NVRAM
17#define DEBUG_NVRAM 0
18#endif
19
20#if DEBUG_NVRAM
21#define DBG(x...) printf(x)
22#else
23#define DBG(x...) msglog(x)
24#endif
25
26/*static const char const FIRMWARE_REVISION_PROP[] = "firmware-revision";
27static const char const FIRMWARE_ABI_PROP[] = "firmware-abi";
28static const char const FIRMWARE_VENDOR_PROP[] = "firmware-vendor";
29static const char const FIRMWARE_ABI_32_PROP_VALUE[] = "EFI32";
30static const char const FIRMWARE_ABI_64_PROP_VALUE[] = "EFI64";
31static const char const SYSTEM_ID_PROP[] = "system-id";
32static const char const SYSTEM_SERIAL_PROP[] = "SystemSerialNumber";
33static const char const SYSTEM_TYPE_PROP[] = "system-type";
34static const char const MODEL_PROP[] = "Model";
35 */
36const char PLATFORM_UUID[] = "platform-uuid";
37//EFI_UINT32 const FIRMWARE_FEATURE_MASK = 0x000003FF;
38EFI_UINT32 const STATIC_ZERO = 0;
39
40#define kBL_GLOBAL_NVRAM_GUID "8BE4DF61-93CA-11D2-AA0D-00E098032B8C"
41
42// Check if a system supports CSM legacy mode
43#define kBL_APPLE_VENDOR_NVRAM_GUID "4D1EDE05-38C7-4A6A-9CC6-4BCCA8B38C14"
44#define UUID_LEN16
45
46//extern EFI_GUID* getSystemID();
47void NVRAM_hook(void* arg1, void* arg2, void* arg3, void* arg4);
48void NVRAM_start(void);
49
50typedef struct {
51char Name[32];
52char Value[512];
53} variables;
54int readNVRAM(variables* v);
55
56
57void NVRAM_hook(void* arg1, void* arg2, void* arg3, void* arg4);
58/*
59void NVRAM_start_hook(void* arg1, void* arg2, void* arg3, void* arg4)
60{
61msglog("NVRAM started with ExecKernel\n");
62
63}
64*/
65
66void NVRAM_start()
67{
68//register_hook_callback("ExecKernel", &NVRAM_start_hook);
69//register_hook_callback("Kernel Start", &NVRAM_hook);
70register_hook_callback("ModulesLoaded", &NVRAM_hook);
71}
72
73void NVRAM_hook(void* arg1, void* arg2, void* arg3, void* arg4)
74{
75charbootName[128];
76variables*var;
77char*ffName;
78uint8_t *FirmwareFeatures;
79EFI_UINT32*FirmwareFeaturesMask;
80char*ffmName;
81char*boName;
82char*bnName;
83//EFI_GUID*ret = 0;
84uint16_tbootOptionNumber = 0;
85int i, j;
86
87//BLESS_EFI_LOAD_OPTION* NextBoot =(BLESS_EFI_LOAD_OPTION*)gBootOrder;
88
89DBG("NVRAM started with ModulesLoaded\n");
90
91//Slice create /options node -> /fakenvram
92// I need to use fakenvram until I know what is happen
93//bool UseNVRAM = FALSE;
94bool ClearNVRAM = FALSE;
95const char* buff;
96//TagPtrdictionary;
97int cnt;
98var = malloc(sizeof(variables)+1);
99ClearNVRAM = getValueForKey("-c", &buff, &cnt, &bootInfo->bootConfig);
100if (!ClearNVRAM) {
101readNVRAM(var);
102}
103
104for (i=0; i<32; i++) {
105if (var[i].Name[0] == 0) {
106break;
107}
108if (strcmp(var[i].Name, "efi-boot-device")==0) {
109//XMLParseFile(var[i].Value, &dictionary);
110//buff = XMLGetProperty(dictionary, "BLLastBSDName")->string;
111verbose("Required boot device is %s\n", var[i].Value);
112//NextBoot->FilePathListLength = 1;
113//strcpy(NextBoot->Description, buff);
114break;
115}
116}
117
118Node* optionsNode = DT__FindNode("/options", true); //"/fakenvram"
119ffName = malloc(sizeof(PLATFORM_UUID)+1);
120strcpy(ffName, PLATFORM_UUID);
121//ret = getSystemID();
122//DT__AddProperty(optionsNode, ffName, UUID_LEN, (EFI_UINT32*) ret);
123
124// this information can be obtained from DMI Type 0
125SMBByte* p = (SMBByte*)FindFirstDmiTableOfType(0, 0x18);
126FirmwareFeatures = malloc(sizeof(SMBByte));
127//TODO - the bufferFF must be composed from bits p[0x12] and [0x13]. Datasheet needed
128*FirmwareFeatures = ((p[19] >> 1) & 1) //USB Legacy is supported
129| ((p[18] >> 14) & 2) //Boot from CD is supported
130| 0x14; //default for bless (GUID partition)
131
132sprintf(bootName, "%s:FirmwareFeatures", kBL_APPLE_VENDOR_NVRAM_GUID);
133ffName = malloc(sizeof(bootName)+1);
134strcpy(ffName, bootName);
135DT__AddProperty(optionsNode, ffName, sizeof(uint32_t), (char *)FirmwareFeatures); //legacy support
136
137sprintf(bootName, "%s:FirmwareFeaturesMask", kBL_APPLE_VENDOR_NVRAM_GUID);
138FirmwareFeaturesMask = malloc(sizeof(EFI_UINT32));
139*FirmwareFeaturesMask = (EFI_UINT32)0x3ff;
140ffmName = malloc(sizeof(bootName)+1);
141strcpy(ffmName, bootName);
142DT__AddProperty(optionsNode, ffmName, sizeof(uint32_t), (EFI_UINT32*)FirmwareFeaturesMask);
143
144//TODO - check, validate and fill by bvr structure.
145//here I am not sure what is BootOrder: node or property?
146//Node* bootNode = DT__AddChild(optionsNode, kBL_GLOBAL_NVRAM_GUID ":BootOrder");
147sprintf(bootName, "%s:BootOrder", kBL_GLOBAL_NVRAM_GUID);
148boName = malloc(sizeof(bootName)+1);
149strcpy(boName, bootName);
150DT__AddProperty(optionsNode, boName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
151
152sprintf(bootName, "%s:Boot%04hx", kBL_GLOBAL_NVRAM_GUID, bootOptionNumber);
153bnName = malloc(sizeof(bootName)+1);
154strcpy(bnName, bootName);
155DT__AddProperty(optionsNode, bnName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
156
157
158//can we add here boot-properties?
159//optionsNode = DT__FindNode("chosen", true);
160#if NOTYET
161int lbC = 0;
162while(((char*)&bootInfo->bootConfig)[lbC++]);
163if (lbC > sizeof(bootInfo->bootConfig)) lbC = sizeof(bootInfo->bootConfig);
164DT__AddProperty(optionsNode, "boot-args", lbC, (char*)&bootInfo->bootConfig);
165#endif
166//TODO - BootCamp emulation?
167/*
168 romNode = DT__FindNode("/rom", true);
169 DT__AddProperty(romNode, "fv-main-address"... //provided by AppleSMBIOS
170 DT__AddProperty(romNode, "fv-main-size"...
171 "IOEFIDevicePathType" -> "MediaFirmwareVolumeFilePath"
172 "Guid" -> "2B0585EB-D8B8-49A9-8B8C-E21B01AEF2B7"
173
174 */
175//
176
177for (i=0; i<32; i++) {
178DBG("NVRAM get a name %s\n", var[i].Name);
179if (var[i].Name[0]) {
180if (isdigit(var[i].Name[0])) {
181msglog(" ...it is digit...\n");
182continue;
183}
184j=0;
185while (var[i].Value[j++]);
186DT__AddProperty(optionsNode, var[i].Name, j,&var[i].Value);
187#if 1 //DEBUG_NVRAM
188DBG("NVRAM add name=%s value=%s length=%d\n", var[i].Name, var[i].Value, j);
189#endif
190} else {
191break;
192}
193
194}
195//Serialization?
196bnName[0] = bnName[1] = 0;
197DT__AddProperty(optionsNode, bnName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
198}
199
200const char NVRAM_INF[] = "nvram.inf";
201int readNVRAM(variables* var)
202{
203int fd, fsize;
204char* nvr = 0;
205DBG("Start NVRAM reading\n");
206if ((fd = open(NVRAM_INF, 0)) < 0) {
207DBG("[ERROR] open NVRAM failed\n");
208return -1;
209}
210fsize = file_size(fd);
211if (!fsize) {
212DBG(" zero NVRAM file\n");
213close (fd);
214return -1;
215}
216if ((nvr = malloc(fsize)) == NULL) {
217DBG("[ERROR] alloc NVRAM memory failed\n");
218close (fd);
219return -1;
220}
221if (read (fd, nvr, fsize) != fsize) {
222DBG("[ERROR] read %s failed\n", NVRAM_INF);
223close (fd);
224return -1;
225}
226close (fd);
227/*if ((var = malloc(fsize)) == NULL) {
228DBG("[ERROR] alloc VAR memory failed\n");
229return -1;
230}*/
231int i = 0;
232char * onvr = nvr;
233bool skipGUID;
234while (*nvr) {
235int j = 0;
236skipGUID = false;
237DBG("Name[%d]:", i);
238while (*nvr != 9) {
239//DBG("%c", *nvr);
240if (*nvr == 4) {
241skipGUID = true; //TODO this is GUID
242nvr++;
243} else
244var[i].Name[j++] = *nvr++;
245}
246DBG("\n");
247nvr++; //skip \09
248var[i].Name[j] = 0; //end of c-string
249if (skipGUID) {
250//TODO this is GUID
251DBG("skip GUID\n");
252while (*nvr++ != 0x0A) {}
253i--;
254} else {
255
256j = 0;
257unsigned char c;
258while (*nvr != 0x0A) {
259c = *nvr++;
260if (c == 0x25) { //TODO this is hex
261int k1=*nvr++;
262if ((k1 >= 0x30) && (k1 <= 0x39)) {
263k1 = k1 - 0x30;
264}
265if ((k1 > 0x60) && (k1 <= 0x66)) {
266k1 = k1 - 0x60 + 10;
267}
268int k2=*nvr++;
269if ((k2 >= 0x30) && (k2 <= 0x39)) {
270k2 = k2 - 0x30;
271}
272if ((k2 > 0x60) && (k2 <= 0x66)) {
273k2 = k2 - 0x60 + 10;
274}
275c = (k1 << 4) + k2;
276//break;
277}
278var[i].Value[j++] = c;
279}
280nvr++;
281DBG("Value[%d]:", i);
282int m;
283for (m=0; m<j; m++) {
284//DBG("%02x ", var[i].Value[m]);
285}
286//DBG("Value[%d]:%s\n", i, var[i].Value);
287DBG("\n");
288}
289i++;
290if ((int)(nvr - onvr) > fsize) {
291DBG("end of buffer\n");
292break;
293}
294}
295var[i].Name[0]=0;
296var[i].Name[1]=0;
297
298return 0;
299}

Archive Download this file

Revision: 1175