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 | ␉␉{␊ |
74 | ␉␉␉dst[0] = src[0];␊ |
75 | ␉␉␉dst[1] = src[1];␊ |
76 | ␉␉␉dst[2] = src[2];␊ |
77 | ␉␉␉dst[3] = src[3];␊ |
78 | ␉␉␉dst[4] = src[4];␊ |
79 | ␉␉␉dst[5] = src[5];␊ |
80 | ␉␉␉dst[6] = src[6];␊ |
81 | ␉␉␉dst[7] = src[7];␊ |
82 | ␉␉␉for (byteCnt = 0; byteCnt < 0x20; byteCnt++) {␊ |
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 | ␉␉printf ("Incorrect image signature\n");␊ |
115 | ␉␉return;␊ |
116 | ␉}␊ |
117 | ␉if (header->encryptStart) {␊ |
118 | ␉␉printf ("Resuming from Encrypted image is unsupported.\n"␊ |
119 | ␉␉␉␉"Uncheck \"Use secure virtual memory\" in \"Security\" pane on system preferences.\n"␊ |
120 | ␉␉␉␉"Press any key to proceed with normal boot.\n");␊ |
121 | ␉␉getchar();␊ |
122 | ␉␉return;␊ |
123 | ␉}␊ |
124 | // depends on NVRAM␊ |
125 | #if 0␊ |
126 | ␉{␊ |
127 | ␉␉uint32_t machineSignature;␊ |
128 | ␉␉size = GetProp(gChosenPH, kIOHibernateMachineSignatureKey,␊ |
129 | ␉␉␉␉␉ (char *)&machineSignature, sizeof(machineSignature));␊ |
130 | ␉␉if (size != sizeof(machineSignature)) {␊ |
131 | ␉␉␉machineSignature = 0;␊ |
132 | ␉␉}␊ |
133 | ␉␉if (machineSignature != header->machineSignature) {␊ |
134 | ␉␉␉break;␊ |
135 | ␉␉}␊ |
136 | ␉}␊ |
137 | #endif␊ |
138 | ␉␊ |
139 | ␉allocSize = imageSize + ((4095 + sizeof(hibernate_graphics_t)) & ~4095);␊ |
140 | ␉␊ |
141 | ␉mem_base = getmemorylimit() - allocSize;//TODO: lower this␊ |
142 | ␉␊ |
143 | ␉printf("mem_base %x\n", mem_base);␊ |
144 | ␉if (((long long)mem_base + allocSize) < (1024 * bootInfo->extmem + 0x100000)) {␊ |
145 | ␉␉printf ("Not enough space to restore image. Press any key to proceed with normal boot.\n");␊ |
146 | ␉␉getchar();␊ |
147 | ␉␉return;␊ |
148 | ␉}␊ |
149 | ␊ |
150 | ␉bcopy(header, (void *) mem_base, sizeof(IOHibernateImageHeader));␊ |
151 | ␉header = (IOHibernateImageHeader *) mem_base;␊ |
152 | ␉␊ |
153 | ␉imageSize -= sizeof(IOHibernateImageHeader);␊ |
154 | ␉buffer = (long)(header + 1);␊ |
155 | ␉␊ |
156 | ␉if (header->previewSize) {␊ |
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 | ␉␉}␊ |
170 | ␉␉previewTotalSectors = 0;␊ |
171 | ␉␉previewLoadedSectors = 0;␊ |
172 | ␉␉previewSaveunder = 0;␊ |
173 | #if 0␊ |
174 | ␉␉AsereBLN:␊ |
175 | ␉␉check_vga_nvidia() didn't work as expected (recursion level > 0 & return value).␊ |
176 | ␉␉Unforutnaltely I cannot find a note why to switch back to text mode for nVidia cards only␊ |
177 | ␉␉and because it check_vga_nvidia does not work (cards normally are behind a bridge) I will␊ |
178 | ␉␉remove it completely␊ |
179 | ␉␉setVideoMode( VGA_TEXT_MODE, 0 );␊ |
180 | #endif␊ |
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 | |