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