Root/
Source at commit 2265 created 10 years 9 months ago. By ifabio, Apply zenith432 patch (Load drivers from /Library/Extensions when using '-f' with OS 10.9) http://forge.voodooprojects.org/p/chameleon/issues/352/ | |
---|---|
1 | /*␊ |
2 | * resume.c␊ |
3 | * ␊ |
4 | *␊ |
5 | * Created by mackerintel on 1/22/09.␊ |
6 | * Copyright 2009 mackerintel. All rights reserved.␊ |
7 | *␊ |
8 | */␊ |
9 | ␊ |
10 | #include "saio_internal.h"␊ |
11 | #include "libsa.h"␊ |
12 | #include "IOHibernatePrivate.h"␊ |
13 | #include "memory.h"␊ |
14 | #include "bootstruct.h"␊ |
15 | #include "boot.h"␊ |
16 | #include "pci.h"␊ |
17 | ␊ |
18 | extern int previewTotalSectors;␊ |
19 | extern int previewLoadedSectors;␊ |
20 | extern uint8_t *previewSaveunder;␊ |
21 | ␊ |
22 | static unsigned long␊ |
23 | getmemorylimit(void)␊ |
24 | {␊ |
25 | int line;␊ |
26 | int i;␊ |
27 | MemoryRange *mp = bootInfo->memoryMap;␊ |
28 | ␊ |
29 | // Activate and clear page 1␊ |
30 | line = 1;␊ |
31 | for (i = 0; i < bootInfo->memoryMapCount; i++)␊ |
32 | {␊ |
33 | if((mp->type == 1) && ((unsigned long)mp->base == 0x100000))␊ |
34 | {␊ |
35 | return (unsigned long)(mp->base + mp->length);␊ |
36 | }␊ |
37 | mp++;␊ |
38 | }␊ |
39 | return 0x10000000;␊ |
40 | }␊ |
41 | ␊ |
42 | static void WakeKernel(IOHibernateImageHeader * header)␊ |
43 | {␊ |
44 | ␉uint32_t proc;␊ |
45 | ␉unsigned long cnt, newSP;␊ |
46 | ␉unsigned long *src, *dst;␊ |
47 | ␉unsigned int ␉count;␊ |
48 | ␉unsigned int ␉page;␊ |
49 | ␉unsigned int ␉compressedSize;␊ |
50 | ␉int32_t ␉byteCnt;␊ |
51 | ␉u_int32_t ␉lowHalf, highHalf;␊ |
52 | ␉u_int32_t ␉sum;␊ |
53 | ␉␊ |
54 | ␉printf("\nWake Kernel!\n");␊ |
55 | ␉␊ |
56 | ␉dst = (unsigned long *) (header->restore1CodePage << 12);␊ |
57 | ␉count = header->restore1PageCount;␊ |
58 | ␉proc = (header->restore1CodeOffset + ((uint32_t) dst));␊ |
59 | ␉newSP = header->restore1StackOffset + (header->restore1CodePage << 12);␊ |
60 | ␉␊ |
61 | ␉src = (unsigned long *) (((u_int32_t) &header->fileExtentMap[0]) ␊ |
62 | ␉␉␉␉␉␉␉ + header->fileExtentMapSize);␊ |
63 | ␉sum = 0;␊ |
64 | ␉␊ |
65 | ␉for (page = 0; page < count; page++)␊ |
66 | ␉{␊ |
67 | ␉␉compressedSize = 4096;␊ |
68 | ␉␉␊ |
69 | ␉␉lowHalf = 1;␊ |
70 | ␉␉highHalf = 0;␊ |
71 | ␉␉␊ |
72 | ␉␉for (cnt = 0; cnt < compressedSize; cnt += 0x20) {␊ |
73 | ␉␉␉dst[0] = src[0];␊ |
74 | ␉␉␉dst[1] = src[1];␊ |
75 | ␉␉␉dst[2] = src[2];␊ |
76 | ␉␉␉dst[3] = src[3];␊ |
77 | ␉␉␉dst[4] = src[4];␊ |
78 | ␉␉␉dst[5] = src[5];␊ |
79 | ␉␉␉dst[6] = src[6];␊ |
80 | ␉␉␉dst[7] = src[7];␊ |
81 | ␉␉␉for (byteCnt = 0; byteCnt < 0x20; byteCnt++) {␊ |
82 | ␉␉␉␉lowHalf += ((u_int8_t *) dst)[byteCnt];␊ |
83 | ␉␉␉␉highHalf += lowHalf;␊ |
84 | ␉␉␉}␊ |
85 | ␉␉␉src += 8;␊ |
86 | ␉␉␉dst += 8;␊ |
87 | ␉␉}␊ |
88 | ␉␉␊ |
89 | ␉␉lowHalf %= 65521L;␊ |
90 | ␉␉highHalf %= 65521L;␊ |
91 | ␉␉sum += (highHalf << 16) | lowHalf;␊ |
92 | ␉}␊ |
93 | ␉header->actualRestore1Sum = sum;␊ |
94 | ␉startprog (proc, header);␊ |
95 | ␉␊ |
96 | ␉return;␊ |
97 | }␊ |
98 | ␊ |
99 | void HibernateBoot(char *image_filename)␊ |
100 | {␊ |
101 | ␉long long size, imageSize, codeSize, allocSize;␊ |
102 | ␉long mem_base;␊ |
103 | ␉IOHibernateImageHeader _header;␊ |
104 | ␉IOHibernateImageHeader * header = &_header;␊ |
105 | ␉long buffer;␊ |
106 | ␉␊ |
107 | ␉size = ReadFileAtOffset (image_filename, header, 0, sizeof(IOHibernateImageHeader));␊ |
108 | ␉printf("header read size %x\n", size);␊ |
109 | ␉␊ |
110 | ␉imageSize = header->image1Size;␊ |
111 | ␉codeSize = header->restore1PageCount << 12;␊ |
112 | ␉if (kIOHibernateHeaderSignature != header->signature)␊ |
113 | ␉{␊ |
114 | ␉␉printf ("Incorrect image signature\n");␊ |
115 | ␉␉return;␊ |
116 | ␉}␊ |
117 | ␉if (header->encryptStart)␊ |
118 | ␉{␊ |
119 | ␉␉printf ("Resuming from Encrypted image is unsupported.\n"␊ |
120 | ␉␉␉␉"Uncheck \"Use secure virtual memory\" in \"Security\" pane on system preferences.\n"␊ |
121 | ␉␉␉␉"Press any key to proceed with normal boot.\n");␊ |
122 | ␉␉getchar();␊ |
123 | ␉␉return;␊ |
124 | ␉}␊ |
125 | // depends on NVRAM␊ |
126 | #if 0␊ |
127 | ␉{␊ |
128 | ␉␉uint32_t machineSignature;␊ |
129 | ␉␉size = GetProp(gChosenPH, kIOHibernateMachineSignatureKey,␊ |
130 | ␉␉␉␉␉ (char *)&machineSignature, sizeof(machineSignature));␊ |
131 | ␉␉if (size != sizeof(machineSignature)) machineSignature = 0;␊ |
132 | ␉␉if (machineSignature != header->machineSignature)␊ |
133 | ␉␉␉break;␊ |
134 | ␉}␊ |
135 | #endif␊ |
136 | ␉␊ |
137 | ␉allocSize = imageSize + ((4095 + sizeof(hibernate_graphics_t)) & ~4095);␊ |
138 | ␉␊ |
139 | ␉mem_base = getmemorylimit() - allocSize;//TODO: lower this␊ |
140 | ␉␊ |
141 | ␉printf("mem_base %x\n", mem_base);␊ |
142 | if (((long long)mem_base + allocSize) < (1024 * bootInfo->extmem + 0x100000))␊ |
143 | ␉{␊ |
144 | ␉␉printf ("Not enough space to restore image. Press any key to proceed with normal boot.\n");␊ |
145 | ␉␉getchar();␊ |
146 | ␉␉return;␊ |
147 | ␉}␊ |
148 | ␉␊ |
149 | ␉bcopy(header, (void *) mem_base, sizeof(IOHibernateImageHeader));␊ |
150 | ␉header = (IOHibernateImageHeader *) mem_base;␊ |
151 | ␉␊ |
152 | ␉imageSize -= sizeof(IOHibernateImageHeader);␊ |
153 | ␉buffer = (long)(header + 1);␊ |
154 | ␉␊ |
155 | ␉if (header->previewSize)␊ |
156 | ␉{␊ |
157 | ␉␉uint64_t preview_offset = header->fileExtentMapSize - sizeof(header->fileExtentMap) + codeSize;␊ |
158 | ␉␉uint8_t progressSaveUnder[kIOHibernateProgressCount][kIOHibernateProgressSaveUnderSize];␊ |
159 | ␉␉␊ |
160 | ␉␉ReadFileAtOffset (image_filename, (char *)buffer, sizeof(IOHibernateImageHeader), preview_offset+header->previewSize);␊ |
161 | ␉␉drawPreview ((void *)(long)(buffer+preview_offset + header->previewPageListSize), &(progressSaveUnder[0][0]));␊ |
162 | ␉␉previewTotalSectors = (imageSize-(preview_offset+header->previewSize))/512;␊ |
163 | ␉␉previewLoadedSectors = 0;␊ |
164 | ␉␉previewSaveunder = &(progressSaveUnder[0][0]);␊ |
165 | ␉␉if (preview_offset+header->previewSize<imageSize)␊ |
166 | ␉␉␉ReadFileAtOffset (image_filename, (char *)(long)(buffer+preview_offset+header->previewSize),␊ |
167 | ␉␉␉␉␉␉␉ sizeof(IOHibernateImageHeader)+preview_offset+header->previewSize,␊ |
168 | ␉␉␉␉␉␉␉ imageSize-(preview_offset+header->previewSize));␊ |
169 | ␉␉previewTotalSectors = 0;␊ |
170 | ␉␉previewLoadedSectors = 0;␊ |
171 | ␉␉previewSaveunder = 0;␊ |
172 | #if 0␊ |
173 | ␉␉AsereBLN:␊ |
174 | ␉␉check_vga_nvidia() didn't work as expected (recursion level > 0 & return value).␊ |
175 | ␉␉Unforutnaltely I cannot find a note why to switch back to text mode for nVidia cards only␊ |
176 | ␉␉and because it check_vga_nvidia does not work (cards normally are behind a bridge) I will␊ |
177 | ␉␉remove it completely␊ |
178 | ␉␉setVideoMode( VGA_TEXT_MODE, 0 );␊ |
179 | #endif␊ |
180 | ␉}␊ |
181 | ␉else␊ |
182 | ␉␉ReadFileAtOffset (image_filename, (char *)buffer, sizeof(IOHibernateImageHeader), imageSize);␊ |
183 | ␉␊ |
184 | // Depends on NVRAM␊ |
185 | #if 0␊ |
186 | ␉if (header->encryptStart) {␊ |
187 | ␉␉// decryption data␊ |
188 | ␉␉static const unsigned char first_iv[AES_BLOCK_SIZE]␊ |
189 | ␉␉= { 0xa3, 0x63, 0x65, 0xa9, 0x0b, 0x71, 0x7b, 0x1c,␊ |
190 | ␉␉0xdf, 0x9e, 0x5f, 0x32, 0xd7, 0x61, 0x63, 0xda };␊ |
191 | ␉␉hibernate_cryptvars_t _cryptvars;␊ |
192 | ␉␉hibernate_cryptvars_t * cryptvars = &_cryptvars;␊ |
193 | ␉␉␊ |
194 | ␉␉aes_decrypt_key(&decryptkey,␊ |
195 | ␉␉␉␉␉␉decryptkeysize,␊ |
196 | ␉␉␉␉␉␉&cryptvars->ctx.decrypt);␊ |
197 | ␉␉␉␊ |
198 | ␉␉// set the vector for the following decryptions␊ |
199 | ␉␉bcopy(((uint8_t *) header) + header->image1Size - AES_BLOCK_SIZE,␊ |
200 | ␉␉␉␉&cryptvars->aes_iv[0], AES_BLOCK_SIZE);␊ |
201 | ␉␉␊ |
202 | ␉␉// decrypt the buffer␊ |
203 | ␉␉uint32_t len = (uint32_t)(header->image1Size - header->encryptStart);␊ |
204 | ␉␉aes_decrypt_cbc(((uint8_t *) header) + header->encryptStart,␊ |
205 | ␉␉␉␉␉␉&first_iv[0],␊ |
206 | ␉␉␉␉␉␉len >> 4,␊ |
207 | ␉␉␉␉␉␉((uint8_t *) header) + header->encryptStart,␊ |
208 | ␉␉␉␉␉␉&cryptvars->ctx.decrypt);␊ |
209 | ␉␉bzero(&cryptvars->aes_iv[0], sizeof(cryptvars));␊ |
210 | ␉␉bzero(&decryptkey, sizeof(decryptkey));␊ |
211 | ␉}␊ |
212 | #endif␊ |
213 | ␉␊ |
214 | ␉WakeKernel(header);␊ |
215 | }␊ |
216 |