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