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;
68msglog("NVRAM is not implemented yet\n");
69return;
70
71msglog("NVRAM started\n");
72
73//Slice create /options node -> /fakenvram
74// I need to use fakenvram until I know what is happen
75//bool UseNVRAM = FALSE;
76bool ClearNVRAM = FALSE;
77const char* buff;
78int cnt;
79ClearNVRAM = getValueForKey(kClearNVRAM, &buff, &cnt, &bootInfo->bootConfig);
80if (!ClearNVRAM) {
81readNVRAM();
82}
83
84Node* optionsNode = DT__FindNode("/options", true);
85ffName = malloc(sizeof(PLATFORM_UUID)+1);
86strcpy(ffName, PLATFORM_UUID);
87ret = getSystemID();
88DT__AddProperty(optionsNode, ffName, UUID_LEN, (EFI_UINT32*) ret);
89
90// this information can be obtained from DMI Type 0
91SMBByte* p = (SMBByte*)FindFirstDmiTableOfType(0, 0x18);
92FirmwareFeatures = malloc(sizeof(SMBByte));
93//TODO - the bufferFF must be composed from bits p[0x12] and [0x13]. Datasheet needed
94*FirmwareFeatures = ((p[19] >> 1) & 1) //USB Legacy is supported
95| ((p[18] >> 14) & 2) //Boot from CD is supported
96| 0x14; //default for bless (GUID partition)
97
98sprintf(bootName, "%s:FirmwareFeatures", kBL_APPLE_VENDOR_NVRAM_GUID);
99ffName = malloc(sizeof(bootName)+1);
100strcpy(ffName, bootName);
101DT__AddProperty(optionsNode, ffName, sizeof(uint32_t), (char *)FirmwareFeatures); //legacy support
102
103sprintf(bootName, "%s:FirmwareFeaturesMask", kBL_APPLE_VENDOR_NVRAM_GUID);
104FirmwareFeaturesMask = malloc(sizeof(EFI_UINT32));
105*FirmwareFeaturesMask = (EFI_UINT32)0x3ff;
106ffmName = malloc(sizeof(bootName)+1);
107strcpy(ffmName, bootName);
108DT__AddProperty(optionsNode, ffmName, sizeof(uint32_t), (EFI_UINT32*)FirmwareFeaturesMask);
109
110//TODO - check, validate and fill by bvr structure.
111//here I am not sure what is BootOrder: node or property?
112//Node* bootNode = DT__AddChild(optionsNode, kBL_GLOBAL_NVRAM_GUID ":BootOrder");
113sprintf(bootName, "%s:BootOrder", kBL_GLOBAL_NVRAM_GUID);
114boName = malloc(sizeof(bootName)+1);
115strcpy(boName, bootName);
116DT__AddProperty(optionsNode, boName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
117
118sprintf(bootName, "%s:Boot%04hx", kBL_GLOBAL_NVRAM_GUID, bootOptionNumber);
119bnName = malloc(sizeof(bootName)+1);
120strcpy(bnName, bootName);
121DT__AddProperty(optionsNode, bnName, sizeof(uint32_t), (EFI_UINT32*)&STATIC_ZERO);
122
123
124//can we add here boot-properties?
125//optionsNode = DT__FindNode("chosen", true);
126#if 1 //NOTYET
127int lbC = 0;
128while(((char*)&bootInfo->bootConfig)[lbC++]);
129if (lbC > sizeof(bootInfo->bootConfig)) lbC = sizeof(bootInfo->bootConfig);
130DT__AddProperty(optionsNode, "boot-args", lbC, (char*)&bootInfo->bootConfig);
131#endif
132//TODO - BootCamp emulation?
133/*
134 romNode = DT__FindNode("/rom", true);
135 DT__AddProperty(romNode, "fv-main-address"... //provided by AppleSMBIOS
136 DT__AddProperty(romNode, "fv-main-size"...
137 "IOEFIDevicePathType" -> "MediaFirmwareVolumeFilePath"
138 "Guid" -> "2B0585EB-D8B8-49A9-8B8C-E21B01AEF2B7"
139
140 */
141//end Slice
142}
143
144void NVRAM_start()
145{
146register_hook_callback("Kernel Start", &NVRAM_hook);
147}
148
149const char NVRAM_INF[] = "nvram.inf";
150int readNVRAM()
151{
152int fd, fsize;
153char* nvr = 0;
154if ((fd = open(NVRAM_INF, 0)) < 0) {
155return -1;
156}
157fsize = file_size(fd);
158if ((nvr = malloc(fsize)) == NULL) {
159verbose("[ERROR] alloc NVRAM memory failed\n");
160close (fd);
161return -1;
162}
163if (read (fd, nvr, fsize) != fsize) {
164verbose("[ERROR] read %s failed\n", NVRAM_INF);
165close (fd);
166return -1;
167}
168close (fd);
169if ((var = malloc(fsize)) == NULL) {
170verbose("[ERROR] alloc VAR memory failed\n");
171return -1;
172}
173int i = 0;
174bool skipGUID;
175while (*nvr) {
176int j = 0;
177skipGUID = false;
178while (*nvr != 9) {
179if (*nvr == 4) {
180skipGUID = true; //TODO this is GUID
181}
182var[i].Name[j++] = *nvr++;
183}
184nvr++; //skip \09
185var[i].Name[j] = 0; //end of c-string
186if (skipGUID) {
187//TODO this is GUID
188while (*nvr++ != 0x0A) {}
189i--;
190} else {
191
192j = 0;
193char c;
194while (*nvr != 0x0A) {
195c = *nvr++;
196if (c == 0x25) { //TODO this is hex
197int k1=*nvr++;
198if ((k1 > 0x30) && (k1 < 0x39)) {
199k1 = k1 - 0x30;
200}
201if ((k1 > 0x60) && (k1 < 0x69)) {
202k1 = k1 - 0x60 + 10;
203}
204int k2=*nvr++;
205if ((k2 > 0x30) && (k2 < 0x39)) {
206k2 = k2 - 0x30;
207}
208if ((k2 > 0x60) && (k2 < 0x69)) {
209k2 = k2 - 0x60 + 10;
210}
211c = (k1 << 4) + k2;
212break;
213}
214var[i].Value[j++] = c;
215}
216}
217i++;
218if (i > fsize) {
219break;
220}
221}
222
223return 0;
224}

Archive Download this file

Revision: 676