Root/
Source at commit HEAD created 5 years 6 days ago. By ifabio, Few update to kernelPatcher (Credits to CrazyBirdy) | |
---|---|
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 | ␊ |
58 | ␉newSP = header->restore1StackOffset + (header->restore1CodePhysPage << 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 | ␉␉␉dst[0] = src[0];␊ |
73 | ␉␉␉dst[1] = src[1];␊ |
74 | ␉␉␉dst[2] = src[2];␊ |
75 | ␉␉␉dst[3] = src[3];␊ |
76 | ␉␉␉dst[4] = src[4];␊ |
77 | ␉␉␉dst[5] = src[5];␊ |
78 | ␉␉␉dst[6] = src[6];␊ |
79 | ␉␉␉dst[7] = src[7];␊ |
80 | ␉␉␉for (byteCnt = 0; byteCnt < 0x20; byteCnt++) {␊ |
81 | ␉␉␉␉lowHalf += ((u_int8_t *) dst)[byteCnt];␊ |
82 | ␉␉␉␉highHalf += lowHalf;␊ |
83 | ␉␉␉}␊ |
84 | ␉␉␉src += 8;␊ |
85 | ␉␉␉dst += 8;␊ |
86 | ␉␉}␊ |
87 | ␊ |
88 | ␉␉lowHalf %= 65521L;␊ |
89 | ␉␉highHalf %= 65521L;␊ |
90 | ␉␉sum += (highHalf << 16) | lowHalf;␊ |
91 | ␉}␊ |
92 | ␉header->actualRestore1Sum = sum;␊ |
93 | ␉startprog (proc, header);␊ |
94 | ␊ |
95 | ␉return;␊ |
96 | }␊ |
97 | ␊ |
98 | void HibernateBoot(char *image_filename)␊ |
99 | {␊ |
100 | ␉long long size, imageSize, codeSize, allocSize;␊ |
101 | ␉long mem_base;␊ |
102 | ␉IOHibernateImageHeader _header;␊ |
103 | ␉IOHibernateImageHeader * header = &_header;␊ |
104 | ␉long buffer;␊ |
105 | ␊ |
106 | ␉size = ReadFileAtOffset (image_filename, header, 0, sizeof(IOHibernateImageHeader));␊ |
107 | ␉printf("header read size %x\n", size);␊ |
108 | ␊ |
109 | ␉imageSize = header->image1Size;␊ |
110 | ␉codeSize = header->restore1PageCount << 12;␊ |
111 | ␉if (kIOHibernateHeaderSignature != header->signature) {␊ |
112 | ␉␉printf ("Incorrect image signature\n");␊ |
113 | ␉␉return;␊ |
114 | ␉}␊ |
115 | ␉if (header->encryptStart) {␊ |
116 | ␉␉printf ("Resuming from Encrypted image is unsupported.\n"␊ |
117 | ␉␉␉␉"Uncheck \"Use secure virtual memory\" in \"Security\" pane on system preferences.\n"␊ |
118 | ␉␉␉␉"Press any key to proceed with normal boot.\n");␊ |
119 | ␉␉getchar();␊ |
120 | ␉␉return;␊ |
121 | ␉}␊ |
122 | // depends on NVRAM␊ |
123 | #if 0␊ |
124 | ␉{␊ |
125 | ␉␉uint32_t machineSignature;␊ |
126 | ␉␉size = GetProp(gChosenPH, kIOHibernateMachineSignatureKey,␊ |
127 | ␉␉␉␉␉ (char *)&machineSignature, sizeof(machineSignature));␊ |
128 | ␉␉if (size != sizeof(machineSignature)) {␊ |
129 | ␉␉␉machineSignature = 0;␊ |
130 | ␉␉}␊ |
131 | ␉␉if (machineSignature != header->machineSignature) {␊ |
132 | ␉␉␉break;␊ |
133 | ␉␉}␊ |
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 | ␉␉printf ("Not enough space to restore image. Press any key to proceed with normal boot.\n");␊ |
144 | ␉␉getchar();␊ |
145 | ␉␉return;␊ |
146 | ␉}␊ |
147 | ␊ |
148 | ␉bcopy(header, (void *) mem_base, sizeof(IOHibernateImageHeader));␊ |
149 | ␉header = (IOHibernateImageHeader *) mem_base;␊ |
150 | ␊ |
151 | ␉imageSize -= sizeof(IOHibernateImageHeader);␊ |
152 | ␉buffer = (long)(header + 1);␊ |
153 | ␉␊ |
154 | ␉if (header->previewSize) {␊ |
155 | ␉␉uint64_t preview_offset = header->fileExtentMapSize - sizeof(header->fileExtentMap) + codeSize;␊ |
156 | ␉␉uint8_t progressSaveUnder[kIOHibernateProgressCount][kIOHibernateProgressSaveUnderSize];␊ |
157 | ␊ |
158 | ␉␉ReadFileAtOffset (image_filename, (char *)buffer, sizeof(IOHibernateImageHeader), preview_offset+header->previewSize);␊ |
159 | ␉␉drawPreview ((void *)(long)(buffer+preview_offset + header->previewPageListSize), &(progressSaveUnder[0][0]));␊ |
160 | ␉␉previewTotalSectors = (imageSize-(preview_offset+header->previewSize))/512;␊ |
161 | ␉␉previewLoadedSectors = 0;␊ |
162 | ␉␉previewSaveunder = &(progressSaveUnder[0][0]);␊ |
163 | ␉␉if (preview_offset+header->previewSize<imageSize) {␊ |
164 | ␉␉␉ReadFileAtOffset (image_filename, (char *)(long)(buffer+preview_offset+header->previewSize),␊ |
165 | ␉␉␉␉␉␉␉ sizeof(IOHibernateImageHeader)+preview_offset+header->previewSize,␊ |
166 | ␉␉␉␉␉␉␉ imageSize-(preview_offset+header->previewSize));␊ |
167 | ␉␉}␊ |
168 | ␉␉previewTotalSectors = 0;␊ |
169 | ␉␉previewLoadedSectors = 0;␊ |
170 | ␉␉previewSaveunder = 0;␊ |
171 | #if 0␊ |
172 | ␉//␉AsereBLN:␊ |
173 | ␉//␉check_vga_nvidia() didn't work as expected (recursion level > 0 & return value).␊ |
174 | ␉//␉Unforutnaltely I cannot find a note why to switch back to text mode for nVidia cards only␊ |
175 | ␉//␉and because it check_vga_nvidia does not work (cards normally are behind a bridge) I will␊ |
176 | ␉//␉remove it completely␊ |
177 | ␉//␉setVideoMode( VGA_TEXT_MODE, 0 );␊ |
178 | #endif␊ |
179 | ␉} else {␊ |
180 | ␉␉ReadFileAtOffset (image_filename, (char *)buffer, sizeof(IOHibernateImageHeader), imageSize);␊ |
181 | ␉}␊ |
182 | // Depends on NVRAM␊ |
183 | #if 0␊ |
184 | ␉if (header->encryptStart) {␊ |
185 | ␉␉// decryption data␊ |
186 | ␉␉static const unsigned char first_iv[AES_BLOCK_SIZE]␊ |
187 | ␉␉= { 0xa3, 0x63, 0x65, 0xa9, 0x0b, 0x71, 0x7b, 0x1c,␊ |
188 | ␉␉0xdf, 0x9e, 0x5f, 0x32, 0xd7, 0x61, 0x63, 0xda };␊ |
189 | ␉␉hibernate_cryptvars_t _cryptvars;␊ |
190 | ␉␉hibernate_cryptvars_t * cryptvars = &_cryptvars;␊ |
191 | ␉␉␊ |
192 | ␉␉aes_decrypt_key(&decryptkey,␊ |
193 | ␉␉␉␉␉␉decryptkeysize,␊ |
194 | ␉␉␉␉␉␉&cryptvars->ctx.decrypt);␊ |
195 | ␉␉␉␊ |
196 | ␉␉// set the vector for the following decryptions␊ |
197 | ␉␉bcopy(((uint8_t *) header) + header->image1Size - AES_BLOCK_SIZE,␊ |
198 | ␉␉␉␉&cryptvars->aes_iv[0], AES_BLOCK_SIZE);␊ |
199 | ␉␉␊ |
200 | ␉␉// decrypt the buffer␊ |
201 | ␉␉uint32_t len = (uint32_t)(header->image1Size - header->encryptStart);␊ |
202 | ␉␉aes_decrypt_cbc(((uint8_t *) header) + header->encryptStart,␊ |
203 | ␉␉␉␉␉␉&first_iv[0],␊ |
204 | ␉␉␉␉␉␉len >> 4,␊ |
205 | ␉␉␉␉␉␉((uint8_t *) header) + header->encryptStart,␊ |
206 | ␉␉␉␉␉␉&cryptvars->ctx.decrypt);␊ |
207 | ␉␉bzero(&cryptvars->aes_iv[0], sizeof(cryptvars));␊ |
208 | ␉␉bzero(&decryptkey, sizeof(decryptkey));␊ |
209 | ␉}␊ |
210 | #endif␊ |
211 | ␊ |
212 | ␉WakeKernel(header);␊ |
213 | }␊ |
214 |