Chameleon

Chameleon Svn Source Tree

Root/branches/slice/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_patcher.h"
14
15#ifndef DEBUG_NVRAM
16#define DEBUG_NVRAM 1
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);
48
49typedef struct {
50char Name[32];
51char Value[512];
52} variables;
53int readNVRAM(variables* v);
54
55
56void NVRAM_hook(void* arg1, void* arg2, void* arg3, void* arg4);
57/*
58void NVRAM_start_hook(void* arg1, void* arg2, void* arg3, void* arg4)
59{
60msglog("NVRAM started with ExecKernel\n");
61
62}
63*/
64
65void NVRAM_start()
66{
67//register_hook_callback("ExecKernel", &NVRAM_start_hook);
68//register_hook_callback("Kernel Start", &NVRAM_hook);
69register_hook_callback("ModulesLoaded", &NVRAM_hook);
70}
71
72void NVRAM_hook(void* arg1, void* arg2, void* arg3, void* arg4)
73{
74charbootName[128];
75variables*var;
76char*ffName;
77uint8_t *FirmwareFeatures;
78EFI_UINT32*FirmwareFeaturesMask;
79char*ffmName;
80char*boName;
81char*bnName;
82EFI_GUID*ret = 0;
83uint16_tbootOptionNumber = 0;
84//msglog("NVRAM is not implemented yet\n");
85
86//return;
87
88msglog("NVRAM started with ModulesLoaded\n");
89
90//Slice create /options node -> /fakenvram
91// I need to use fakenvram until I know what is happen
92//bool UseNVRAM = FALSE;
93bool ClearNVRAM = FALSE;
94const char* buff;
95int cnt;
96var = malloc(sizeof(variables)+1);
97ClearNVRAM = getValueForKey(kClearNVRAM, &buff, &cnt, &bootInfo->bootConfig);
98if (!ClearNVRAM) {
99readNVRAM(var);
100}
101
102Node* optionsNode = DT__FindNode("/options", true);
103ffName = malloc(sizeof(PLATFORM_UUID)+1);
104strcpy(ffName, PLATFORM_UUID);
105ret = getSystemID();
106DT__AddProperty(optionsNode, ffName, UUID_LEN, (EFI_UINT32*) ret);
107
108// this information can be obtained from DMI Type 0
109SMBByte* p = (SMBByte*)FindFirstDmiTableOfType(0, 0x18);
110FirmwareFeatures = malloc(sizeof(SMBByte));
111//TODO - the bufferFF must be composed from bits p[0x12] and [0x13]. Datasheet needed
112*FirmwareFeatures = ((p[19] >> 1) & 1) //USB Legacy is supported
113| ((p[18] >> 14) & 2) //Boot from CD is supported
114| 0x14; //default for bless (GUID partition)
115
116sprintf(bootName, "%s:FirmwareFeatures", kBL_APPLE_VENDOR_NVRAM_GUID);
117ffName = malloc(sizeof(bootName)+1);
118strcpy(ffName, bootName);
119DT__AddProperty(optionsNode, ffName, sizeof(uint32_t), (char *)FirmwareFeatures); //legacy support
120
121sprintf(bootName, "%s:FirmwareFeaturesMask", kBL_APPLE_VENDOR_NVRAM_GUID);
122FirmwareFeaturesMask = malloc(sizeof(EFI_UINT32));
123*FirmwareFeaturesMask = (EFI_UINT32)0x3ff;
124ffmName = malloc(sizeof(bootName)+1);
125strcpy(ffmName, bootName);
126DT__AddProperty(optionsNode, ffmName, sizeof(uint32_t), (EFI_UINT32*)FirmwareFeaturesMask);
127
128//TODO - check, validate and fill by bvr structure.
129//here I am not sure what is BootOrder: node or property?
130//Node* bootNode = DT__AddChild(optionsNode, kBL_GLOBAL_NVRAM_GUID ":BootOrder");
131sprintf(bootName, "%s:BootOrder", kBL_GLOBAL_NVRAM_GUID);
132boName = malloc(sizeof(bootName)+1);
133strcpy(boName, bootName);
134DT__AddProperty(optionsNode, boName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
135
136sprintf(bootName, "%s:Boot%04hx", kBL_GLOBAL_NVRAM_GUID, bootOptionNumber);
137bnName = malloc(sizeof(bootName)+1);
138strcpy(bnName, bootName);
139DT__AddProperty(optionsNode, bnName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
140
141
142//can we add here boot-properties?
143//optionsNode = DT__FindNode("chosen", true);
144#if 1 //NOTYET
145int lbC = 0;
146while(((char*)&bootInfo->bootConfig)[lbC++]);
147if (lbC > sizeof(bootInfo->bootConfig)) lbC = sizeof(bootInfo->bootConfig);
148DT__AddProperty(optionsNode, "boot-args", lbC, (char*)&bootInfo->bootConfig);
149#endif
150//TODO - BootCamp emulation?
151/*
152 romNode = DT__FindNode("/rom", true);
153 DT__AddProperty(romNode, "fv-main-address"... //provided by AppleSMBIOS
154 DT__AddProperty(romNode, "fv-main-size"...
155 "IOEFIDevicePathType" -> "MediaFirmwareVolumeFilePath"
156 "Guid" -> "2B0585EB-D8B8-49A9-8B8C-E21B01AEF2B7"
157
158 */
159//end Slice
160int i,j;
161for (i=0; i<32; i++) {
162if (var[i].Name[0]) {
163msglog("NVRAM get a name %s\n", var[i].Name);
164if (isdigit(var[i].Name[0])) {
165msglog(" ...it is digit...\n");
166continue;
167}
168j=0;
169while (var[i].Value[j++]);
170DT__AddProperty(optionsNode, var[i].Name, j,&var[i].Value);
171#if DEBUG_NVRAM
172msglog("NVRAM add name=%s value=%s length=%d\n", var[i].Name, var[i].Value, j);
173#endif
174} else {
175return;
176}
177
178}
179}
180
181const char NVRAM_INF[] = "nvram.inf";
182int readNVRAM(variables* var)
183{
184int fd, fsize;
185char* nvr = 0;
186msglog("Start NVRAM reading\n");
187if ((fd = open(NVRAM_INF, 0)) < 0) {
188msglog("[ERROR] open NVRAM failed\n");
189return -1;
190}
191fsize = file_size(fd);
192if (!fsize) {
193msglog(" zero NVRAM file\n");
194close (fd);
195return -1;
196}
197if ((nvr = malloc(fsize)) == NULL) {
198verbose("[ERROR] alloc NVRAM memory failed\n");
199close (fd);
200return -1;
201}
202if (read (fd, nvr, fsize) != fsize) {
203verbose("[ERROR] read %s failed\n", NVRAM_INF);
204close (fd);
205return -1;
206}
207close (fd);
208if ((var = malloc(fsize)) == NULL) {
209verbose("[ERROR] alloc VAR memory failed\n");
210return -1;
211}
212int i = 0;
213bool skipGUID;
214while (*nvr) {
215int j = 0;
216skipGUID = false;
217while (*nvr != 9) {
218if (*nvr == 4) {
219skipGUID = true; //TODO this is GUID
220}
221var[i].Name[j++] = *nvr++;
222}
223nvr++; //skip \09
224var[i].Name[j] = 0; //end of c-string
225if (skipGUID) {
226//TODO this is GUID
227while (*nvr++ != 0x0A) {}
228i--;
229} else {
230
231j = 0;
232char c;
233while (*nvr != 0x0A) {
234c = *nvr++;
235if (c == 0x25) { //TODO this is hex
236int k1=*nvr++;
237if ((k1 >= 0x30) && (k1 <= 0x39)) {
238k1 = k1 - 0x30;
239}
240if ((k1 > 0x60) && (k1 <= 0x66)) {
241k1 = k1 - 0x60 + 10;
242}
243int k2=*nvr++;
244if ((k2 >= 0x30) && (k2 <= 0x39)) {
245k2 = k2 - 0x30;
246}
247if ((k2 > 0x60) && (k2 <= 0x66)) {
248k2 = k2 - 0x60 + 10;
249}
250c = (k1 << 4) + k2;
251break;
252}
253var[i].Value[j++] = c;
254}
255}
256i++;
257if (i > fsize) {
258break;
259}
260}
261
262return 0;
263}

Archive Download this file

Revision: 691