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