]>
Commit | Line | Data |
---|---|---|
82705b6e MT |
1 | --- grub-0.97/stage2/shared.h.nxstack 2005-12-12 18:31:41.000000000 -0500 |
2 | +++ grub-0.97/stage2/shared.h 2005-12-12 18:31:42.000000000 -0500 | |
3 | @@ -36,8 +36,8 @@ | |
4 | ||
5 | /* Maybe redirect memory requests through grub_scratch_mem. */ | |
6 | #ifdef GRUB_UTIL | |
7 | -extern char *grub_scratch_mem; | |
8 | -# define RAW_ADDR(x) ((x) + (int) grub_scratch_mem) | |
9 | +extern void *grub_scratch_mem; | |
10 | +# define RAW_ADDR(x) ((x) + (unsigned long) grub_scratch_mem) | |
11 | # define RAW_SEG(x) (RAW_ADDR ((x) << 4) >> 4) | |
12 | #else | |
13 | # define RAW_ADDR(x) (x) | |
14 | --- grub-0.97/stage2/builtins.c.nxstack 2005-12-12 18:31:41.000000000 -0500 | |
15 | +++ grub-0.97/stage2/builtins.c 2005-12-12 18:41:47.000000000 -0500 | |
16 | @@ -131,62 +131,97 @@ | |
17 | } | |
18 | ||
19 | \f | |
20 | +/* blocklist_read_helper nee disk_read_blocklist_func was a nested | |
21 | + * function, to which pointers were taken and exposed globally. Even | |
22 | + * in the GNU-C nested functions extension, they have local linkage, | |
23 | + * and aren't guaranteed to be accessable *at all* outside of their | |
24 | + * containing scope. | |
25 | + * | |
26 | + * Above and beyond all of that, the variables within blocklist_func_context | |
27 | + * are originally local variables, with local (not even static) linkage, | |
28 | + * from within blocklist_func. These were each referenced by | |
29 | + * disk_read_blocklist_func, which is only called from other functions | |
30 | + * through a globally scoped pointer. | |
31 | + * | |
32 | + * The documentation in GCC actually uses the words "all hell will break | |
33 | + * loose" to describe this scenario. | |
34 | + * | |
35 | + * Also, "start_sector" was also used uninitialized, but gcc doesn't warn | |
36 | + * about it (possibly because of the scoping madness?) | |
37 | + */ | |
38 | + | |
39 | +static struct { | |
40 | + int start_sector; | |
41 | + int num_sectors; | |
42 | + int num_entries; | |
43 | + int last_length; | |
44 | +} blocklist_func_context = { | |
45 | + .start_sector = 0, | |
46 | + .num_sectors = 0, | |
47 | + .num_entries = 0, | |
48 | + .last_length = 0 | |
49 | +}; | |
50 | + | |
51 | +/* Collect contiguous blocks into one entry as many as possible, | |
52 | + and print the blocklist notation on the screen. */ | |
53 | +static void | |
54 | +blocklist_read_helper (int sector, int offset, int length) | |
55 | +{ | |
56 | + int *start_sector = &blocklist_func_context.start_sector; | |
57 | + int *num_sectors = &blocklist_func_context.num_sectors; | |
58 | + int *num_entries = &blocklist_func_context.num_entries; | |
59 | + int *last_length = &blocklist_func_context.last_length; | |
60 | + | |
61 | + if (*num_sectors > 0) | |
62 | + { | |
63 | + if (*start_sector + *num_sectors == sector | |
64 | + && offset == 0 && *last_length == SECTOR_SIZE) | |
65 | + { | |
66 | + *num_sectors++; | |
67 | + *last_length = length; | |
68 | + return; | |
69 | + } | |
70 | + else | |
71 | + { | |
72 | + if (*last_length == SECTOR_SIZE) | |
73 | + grub_printf ("%s%d+%d", *num_entries ? "," : "", | |
74 | + *start_sector - part_start, *num_sectors); | |
75 | + else if (*num_sectors > 1) | |
76 | + grub_printf ("%s%d+%d,%d[0-%d]", *num_entries ? "," : "", | |
77 | + *start_sector - part_start, *num_sectors-1, | |
78 | + *start_sector + *num_sectors-1 - part_start, | |
79 | + *last_length); | |
80 | + else | |
81 | + grub_printf ("%s%d[0-%d]", *num_entries ? "," : "", | |
82 | + *start_sector - part_start, *last_length); | |
83 | + *num_entries++; | |
84 | + *num_sectors = 0; | |
85 | + } | |
86 | + } | |
87 | + | |
88 | + if (offset > 0) | |
89 | + { | |
90 | + grub_printf("%s%d[%d-%d]", *num_entries ? "," : "", | |
91 | + sector-part_start, offset, offset+length); | |
92 | + *num_entries++; | |
93 | + } | |
94 | + else | |
95 | + { | |
96 | + *start_sector = sector; | |
97 | + *num_sectors = 1; | |
98 | + *last_length = length; | |
99 | + } | |
100 | +} | |
101 | + | |
102 | /* blocklist */ | |
103 | static int | |
104 | blocklist_func (char *arg, int flags) | |
105 | { | |
106 | char *dummy = (char *) RAW_ADDR (0x100000); | |
107 | - int start_sector; | |
108 | - int num_sectors = 0; | |
109 | - int num_entries = 0; | |
110 | - int last_length = 0; | |
111 | ||
112 | - auto void disk_read_blocklist_func (int sector, int offset, int length); | |
113 | - | |
114 | - /* Collect contiguous blocks into one entry as many as possible, | |
115 | - and print the blocklist notation on the screen. */ | |
116 | - auto void disk_read_blocklist_func (int sector, int offset, int length) | |
117 | - { | |
118 | - if (num_sectors > 0) | |
119 | - { | |
120 | - if (start_sector + num_sectors == sector | |
121 | - && offset == 0 && last_length == SECTOR_SIZE) | |
122 | - { | |
123 | - num_sectors++; | |
124 | - last_length = length; | |
125 | - return; | |
126 | - } | |
127 | - else | |
128 | - { | |
129 | - if (last_length == SECTOR_SIZE) | |
130 | - grub_printf ("%s%d+%d", num_entries ? "," : "", | |
131 | - start_sector - part_start, num_sectors); | |
132 | - else if (num_sectors > 1) | |
133 | - grub_printf ("%s%d+%d,%d[0-%d]", num_entries ? "," : "", | |
134 | - start_sector - part_start, num_sectors-1, | |
135 | - start_sector + num_sectors-1 - part_start, | |
136 | - last_length); | |
137 | - else | |
138 | - grub_printf ("%s%d[0-%d]", num_entries ? "," : "", | |
139 | - start_sector - part_start, last_length); | |
140 | - num_entries++; | |
141 | - num_sectors = 0; | |
142 | - } | |
143 | - } | |
144 | - | |
145 | - if (offset > 0) | |
146 | - { | |
147 | - grub_printf("%s%d[%d-%d]", num_entries ? "," : "", | |
148 | - sector-part_start, offset, offset+length); | |
149 | - num_entries++; | |
150 | - } | |
151 | - else | |
152 | - { | |
153 | - start_sector = sector; | |
154 | - num_sectors = 1; | |
155 | - last_length = length; | |
156 | - } | |
157 | - } | |
158 | + int *start_sector = &blocklist_func_context.start_sector; | |
159 | + int *num_sectors = &blocklist_func_context.num_sectors; | |
160 | + int *num_entries = &blocklist_func_context.num_entries; | |
161 | ||
162 | /* Open the file. */ | |
163 | if (! grub_open (arg)) | |
164 | @@ -206,15 +241,15 @@ | |
165 | grub_printf (")"); | |
166 | ||
167 | /* Read in the whole file to DUMMY. */ | |
168 | - disk_read_hook = disk_read_blocklist_func; | |
169 | + disk_read_hook = blocklist_read_helper; | |
170 | if (! grub_read (dummy, -1)) | |
171 | goto fail; | |
172 | ||
173 | /* The last entry may not be printed yet. Don't check if it is a | |
174 | * full sector, since it doesn't matter if we read too much. */ | |
175 | - if (num_sectors > 0) | |
176 | - grub_printf ("%s%d+%d", num_entries ? "," : "", | |
177 | - start_sector - part_start, num_sectors); | |
178 | + if (*num_sectors > 0) | |
179 | + grub_printf ("%s%d+%d", *num_entries ? "," : "", | |
180 | + *start_sector - part_start, *num_sectors); | |
181 | ||
182 | grub_printf ("\n"); | |
183 | ||
184 | @@ -1889,6 +1924,77 @@ | |
185 | ||
186 | \f | |
187 | /* install */ | |
188 | +static struct { | |
189 | + int saved_sector; | |
190 | + int installaddr; | |
191 | + int installlist; | |
192 | + char *stage2_first_buffer; | |
193 | +} install_func_context = { | |
194 | + .saved_sector = 0, | |
195 | + .installaddr = 0, | |
196 | + .installlist = 0, | |
197 | + .stage2_first_buffer = NULL, | |
198 | +}; | |
199 | + | |
200 | +/* Save the first sector of Stage2 in STAGE2_SECT. */ | |
201 | +/* Formerly disk_read_savesect_func with local scope inside install_func */ | |
202 | +static void | |
203 | +install_savesect_helper(int sector, int offset, int length) | |
204 | +{ | |
205 | + if (debug) | |
206 | + printf ("[%d]", sector); | |
207 | + | |
208 | + /* ReiserFS has files which sometimes contain data not aligned | |
209 | + on sector boundaries. Returning an error is better than | |
210 | + silently failing. */ | |
211 | + if (offset != 0 || length != SECTOR_SIZE) | |
212 | + errnum = ERR_UNALIGNED; | |
213 | + | |
214 | + install_func_context.saved_sector = sector; | |
215 | +} | |
216 | + | |
217 | +/* Write SECTOR to INSTALLLIST, and update INSTALLADDR and INSTALLSECT. */ | |
218 | +/* Formerly disk_read_blocklist_func with local scope inside install_func */ | |
219 | +static void | |
220 | +install_blocklist_helper (int sector, int offset, int length) | |
221 | +{ | |
222 | + int *installaddr = &install_func_context.installaddr; | |
223 | + int *installlist = &install_func_context.installlist; | |
224 | + char **stage2_first_buffer = &install_func_context.stage2_first_buffer; | |
225 | + /* Was the last sector full? */ | |
226 | + static int last_length = SECTOR_SIZE; | |
227 | + | |
228 | + if (debug) | |
229 | + printf("[%d]", sector); | |
230 | + | |
231 | + if (offset != 0 || last_length != SECTOR_SIZE) | |
232 | + { | |
233 | + /* We found a non-sector-aligned data block. */ | |
234 | + errnum = ERR_UNALIGNED; | |
235 | + return; | |
236 | + } | |
237 | + | |
238 | + last_length = length; | |
239 | + | |
240 | + if (*((unsigned long *) (*installlist - 4)) | |
241 | + + *((unsigned short *) *installlist) != sector | |
242 | + || *installlist == (int) *stage2_first_buffer + SECTOR_SIZE + 4) | |
243 | + { | |
244 | + *installlist -= 8; | |
245 | + | |
246 | + if (*((unsigned long *) (*installlist - 8))) | |
247 | + errnum = ERR_WONT_FIT; | |
248 | + else | |
249 | + { | |
250 | + *((unsigned short *) (*installlist + 2)) = (*installaddr >> 4); | |
251 | + *((unsigned long *) (*installlist - 4)) = sector; | |
252 | + } | |
253 | + } | |
254 | + | |
255 | + *((unsigned short *) *installlist) += 1; | |
256 | + *installaddr += 512; | |
257 | +} | |
258 | + | |
259 | static int | |
260 | install_func (char *arg, int flags) | |
261 | { | |
262 | @@ -1896,8 +2002,12 @@ | |
263 | char *stage1_buffer = (char *) RAW_ADDR (0x100000); | |
264 | char *stage2_buffer = stage1_buffer + SECTOR_SIZE; | |
265 | char *old_sect = stage2_buffer + SECTOR_SIZE; | |
266 | - char *stage2_first_buffer = old_sect + SECTOR_SIZE; | |
267 | - char *stage2_second_buffer = stage2_first_buffer + SECTOR_SIZE; | |
268 | + /* stage2_first_buffer used to be defined as: | |
269 | + * char *stage2_first_buffer = old_sect + SECTOR_SIZE; */ | |
270 | + char **stage2_first_buffer = &install_func_context.stage2_first_buffer; | |
271 | + /* and stage2_second_buffer was: | |
272 | + * char *stage2_second_buffer = stage2_first_buffer + SECTOR_SIZE; */ | |
273 | + char *stage2_second_buffer = old_sect + SECTOR_SIZE + SECTOR_SIZE; | |
274 | /* XXX: Probably SECTOR_SIZE is reasonable. */ | |
275 | char *config_filename = stage2_second_buffer + SECTOR_SIZE; | |
276 | char *dummy = config_filename + SECTOR_SIZE; | |
277 | @@ -1906,10 +2016,11 @@ | |
278 | int src_drive, src_partition, src_part_start; | |
279 | int i; | |
280 | struct geometry dest_geom, src_geom; | |
281 | - int saved_sector; | |
282 | + int *saved_sector = &install_func_context.saved_sector; | |
283 | int stage2_first_sector, stage2_second_sector; | |
284 | char *ptr; | |
285 | - int installaddr, installlist; | |
286 | + int *installaddr = &install_func_context.installaddr; | |
287 | + int *installlist = &install_func_context.installlist; | |
288 | /* Point to the location of the name of a configuration file in Stage 2. */ | |
289 | char *config_file_location; | |
290 | /* If FILE is a Stage 1.5? */ | |
291 | @@ -1918,68 +2029,14 @@ | |
292 | int is_open = 0; | |
293 | /* If LBA is forced? */ | |
294 | int is_force_lba = 0; | |
295 | - /* Was the last sector full? */ | |
296 | - int last_length = SECTOR_SIZE; | |
297 | ||
298 | + *stage2_first_buffer = old_sect + SECTOR_SIZE; | |
299 | #ifdef GRUB_UTIL | |
300 | /* If the Stage 2 is in a partition mounted by an OS, this will store | |
301 | the filename under the OS. */ | |
302 | char *stage2_os_file = 0; | |
303 | #endif /* GRUB_UTIL */ | |
304 | ||
305 | - auto void disk_read_savesect_func (int sector, int offset, int length); | |
306 | - auto void disk_read_blocklist_func (int sector, int offset, int length); | |
307 | - | |
308 | - /* Save the first sector of Stage2 in STAGE2_SECT. */ | |
309 | - auto void disk_read_savesect_func (int sector, int offset, int length) | |
310 | - { | |
311 | - if (debug) | |
312 | - printf ("[%d]", sector); | |
313 | - | |
314 | - /* ReiserFS has files which sometimes contain data not aligned | |
315 | - on sector boundaries. Returning an error is better than | |
316 | - silently failing. */ | |
317 | - if (offset != 0 || length != SECTOR_SIZE) | |
318 | - errnum = ERR_UNALIGNED; | |
319 | - | |
320 | - saved_sector = sector; | |
321 | - } | |
322 | - | |
323 | - /* Write SECTOR to INSTALLLIST, and update INSTALLADDR and | |
324 | - INSTALLSECT. */ | |
325 | - auto void disk_read_blocklist_func (int sector, int offset, int length) | |
326 | - { | |
327 | - if (debug) | |
328 | - printf("[%d]", sector); | |
329 | - | |
330 | - if (offset != 0 || last_length != SECTOR_SIZE) | |
331 | - { | |
332 | - /* We found a non-sector-aligned data block. */ | |
333 | - errnum = ERR_UNALIGNED; | |
334 | - return; | |
335 | - } | |
336 | - | |
337 | - last_length = length; | |
338 | - | |
339 | - if (*((unsigned long *) (installlist - 4)) | |
340 | - + *((unsigned short *) installlist) != sector | |
341 | - || installlist == (int) stage2_first_buffer + SECTOR_SIZE + 4) | |
342 | - { | |
343 | - installlist -= 8; | |
344 | - | |
345 | - if (*((unsigned long *) (installlist - 8))) | |
346 | - errnum = ERR_WONT_FIT; | |
347 | - else | |
348 | - { | |
349 | - *((unsigned short *) (installlist + 2)) = (installaddr >> 4); | |
350 | - *((unsigned long *) (installlist - 4)) = sector; | |
351 | - } | |
352 | - } | |
353 | - | |
354 | - *((unsigned short *) installlist) += 1; | |
355 | - installaddr += 512; | |
356 | - } | |
357 | - | |
358 | /* First, check the GNU-style long option. */ | |
359 | while (1) | |
360 | { | |
361 | @@ -2011,10 +2068,10 @@ | |
362 | addr = skip_to (0, file); | |
363 | ||
364 | /* Get the installation address. */ | |
365 | - if (! safe_parse_maxint (&addr, &installaddr)) | |
366 | + if (! safe_parse_maxint (&addr, installaddr)) | |
367 | { | |
368 | /* ADDR is not specified. */ | |
369 | - installaddr = 0; | |
370 | + *installaddr = 0; | |
371 | ptr = addr; | |
372 | errnum = 0; | |
373 | } | |
374 | @@ -2110,17 +2167,17 @@ | |
375 | = 0x9090; | |
376 | ||
377 | /* Read the first sector of Stage 2. */ | |
378 | - disk_read_hook = disk_read_savesect_func; | |
379 | - if (grub_read (stage2_first_buffer, SECTOR_SIZE) != SECTOR_SIZE) | |
380 | + disk_read_hook = install_savesect_helper; | |
381 | + if (grub_read (*stage2_first_buffer, SECTOR_SIZE) != SECTOR_SIZE) | |
382 | goto fail; | |
383 | ||
384 | - stage2_first_sector = saved_sector; | |
385 | + stage2_first_sector = *saved_sector; | |
386 | ||
387 | /* Read the second sector of Stage 2. */ | |
388 | if (grub_read (stage2_second_buffer, SECTOR_SIZE) != SECTOR_SIZE) | |
389 | goto fail; | |
390 | ||
391 | - stage2_second_sector = saved_sector; | |
392 | + stage2_second_sector = *saved_sector; | |
393 | ||
394 | /* Check for the version of Stage 2. */ | |
395 | if (*((short *) (stage2_second_buffer + STAGE2_VER_MAJ_OFFS)) | |
396 | @@ -2136,27 +2193,27 @@ | |
397 | ||
398 | /* If INSTALLADDR is not specified explicitly in the command-line, | |
399 | determine it by the Stage 2 id. */ | |
400 | - if (! installaddr) | |
401 | + if (! *installaddr) | |
402 | { | |
403 | if (! is_stage1_5) | |
404 | /* Stage 2. */ | |
405 | - installaddr = 0x8000; | |
406 | + *installaddr = 0x8000; | |
407 | else | |
408 | /* Stage 1.5. */ | |
409 | - installaddr = 0x2000; | |
410 | + *installaddr = 0x2000; | |
411 | } | |
412 | ||
413 | *((unsigned long *) (stage1_buffer + STAGE1_STAGE2_SECTOR)) | |
414 | = stage2_first_sector; | |
415 | *((unsigned short *) (stage1_buffer + STAGE1_STAGE2_ADDRESS)) | |
416 | - = installaddr; | |
417 | + = *installaddr; | |
418 | *((unsigned short *) (stage1_buffer + STAGE1_STAGE2_SEGMENT)) | |
419 | - = installaddr >> 4; | |
420 | + = *installaddr >> 4; | |
421 | ||
422 | - i = (int) stage2_first_buffer + SECTOR_SIZE - 4; | |
423 | + i = (int) *stage2_first_buffer + SECTOR_SIZE - 4; | |
424 | while (*((unsigned long *) i)) | |
425 | { | |
426 | - if (i < (int) stage2_first_buffer | |
427 | + if (i < (int) *stage2_first_buffer | |
428 | || (*((int *) (i - 4)) & 0x80000000) | |
429 | || *((unsigned short *) i) >= 0xA00 | |
430 | || *((short *) (i + 2)) == 0) | |
431 | @@ -2170,13 +2227,13 @@ | |
432 | i -= 8; | |
433 | } | |
434 | ||
435 | - installlist = (int) stage2_first_buffer + SECTOR_SIZE + 4; | |
436 | - installaddr += SECTOR_SIZE; | |
437 | + *installlist = (int) *stage2_first_buffer + SECTOR_SIZE + 4; | |
438 | + *installaddr += SECTOR_SIZE; | |
439 | ||
440 | /* Read the whole of Stage2 except for the first sector. */ | |
441 | grub_seek (SECTOR_SIZE); | |
442 | ||
443 | - disk_read_hook = disk_read_blocklist_func; | |
444 | + disk_read_hook = install_blocklist_helper; | |
445 | if (! grub_read (dummy, -1)) | |
446 | goto fail; | |
447 | ||
448 | @@ -2259,7 +2316,7 @@ | |
449 | /* Skip the first sector. */ | |
450 | grub_seek (SECTOR_SIZE); | |
451 | ||
452 | - disk_read_hook = disk_read_savesect_func; | |
453 | + disk_read_hook = install_savesect_helper; | |
454 | if (grub_read (stage2_buffer, SECTOR_SIZE) != SECTOR_SIZE) | |
455 | goto fail; | |
456 | ||
457 | @@ -2329,7 +2386,7 @@ | |
458 | else | |
459 | #endif /* GRUB_UTIL */ | |
460 | { | |
461 | - if (! devwrite (saved_sector - part_start, 1, stage2_buffer)) | |
462 | + if (! devwrite (*saved_sector - part_start, 1, stage2_buffer)) | |
463 | goto fail; | |
464 | } | |
465 | } | |
466 | @@ -2351,7 +2408,7 @@ | |
467 | goto fail; | |
468 | } | |
469 | ||
470 | - if (fwrite (stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE) | |
471 | + if (fwrite (*stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE) | |
472 | { | |
473 | fclose (fp); | |
474 | errnum = ERR_WRITE; | |
475 | @@ -2378,7 +2435,7 @@ | |
476 | goto fail; | |
477 | ||
478 | if (! devwrite (stage2_first_sector - src_part_start, 1, | |
479 | - stage2_first_buffer)) | |
480 | + *stage2_first_buffer)) | |
481 | goto fail; | |
482 | ||
483 | if (! devwrite (stage2_second_sector - src_part_start, 1, | |
484 | --- grub-0.97/grub/asmstub.c.nxstack 2005-12-12 18:31:41.000000000 -0500 | |
485 | +++ grub-0.97/grub/asmstub.c 2005-12-12 18:31:42.000000000 -0500 | |
486 | @@ -42,6 +42,7 @@ | |
487 | #include <sys/time.h> | |
488 | #include <termios.h> | |
489 | #include <signal.h> | |
490 | +#include <sys/mman.h> | |
491 | ||
492 | #ifdef __linux__ | |
493 | # include <sys/ioctl.h> /* ioctl */ | |
494 | @@ -82,7 +83,7 @@ | |
495 | struct apm_info apm_bios_info; | |
496 | ||
497 | /* Emulation requirements. */ | |
498 | -char *grub_scratch_mem = 0; | |
499 | +void *grub_scratch_mem = 0; | |
500 | ||
501 | struct geometry *disks = 0; | |
502 | ||
503 | @@ -106,14 +107,62 @@ | |
504 | static unsigned int serial_speed; | |
505 | #endif /* SIMULATE_SLOWNESS_OF_SERIAL */ | |
506 | ||
507 | +/* This allocates page-aligned storage of the specified size, which must be | |
508 | + * a multiple of the page size as determined by calling sysconf(_SC_PAGESIZE) | |
509 | + */ | |
510 | +#ifdef __linux__ | |
511 | +static void * | |
512 | +grub_mmap_alloc(size_t len) | |
513 | +{ | |
514 | + int mmap_flags = MAP_ANONYMOUS|MAP_PRIVATE|MAP_EXECUTABLE; | |
515 | + | |
516 | +#ifdef MAP_32BIT | |
517 | + mmap_flags |= MAP_32BIT; | |
518 | +#endif | |
519 | + /* Mark the simulated stack executable, as GCC uses stack trampolines | |
520 | + * to implement nested functions. */ | |
521 | + return mmap(NULL, len, PROT_READ|PROT_WRITE|PROT_EXEC, mmap_flags, -1, 0); | |
522 | +} | |
523 | +#else /* !defined(__linux__) */ | |
524 | +static void * | |
525 | +grub_mmap_alloc(size_t len) | |
526 | +{ | |
527 | + int fd = 0, offset = 0, ret = 0; | |
528 | + void *pa = MAP_FAILED; | |
529 | + char template[] = "/tmp/grub_mmap_alloc_XXXXXX"; | |
530 | + errno_t e; | |
531 | + | |
532 | + fd = mkstemp(template); | |
533 | + if (fd < 0) | |
534 | + return pa; | |
535 | + | |
536 | + unlink(template); | |
537 | + | |
538 | + ret = ftruncate(fd, len); | |
539 | + if (ret < 0) | |
540 | + return pa; | |
541 | + | |
542 | + /* Mark the simulated stack executable, as GCC uses stack trampolines | |
543 | + * to implement nested functions. */ | |
544 | + pa = mmap(NULL, len, PROT_READ|PROT_WRITE|PROT_EXEC, | |
545 | + MAP_PRIVATE|MAP_EXECUTABLE, fd, offset); | |
546 | + | |
547 | + e = errno; | |
548 | + close(fd); | |
549 | + errno = e; | |
550 | + return pa; | |
551 | +} | |
552 | +#endif /* defined(__linux__) */ | |
553 | + | |
554 | /* The main entry point into this mess. */ | |
555 | int | |
556 | grub_stage2 (void) | |
557 | { | |
558 | /* These need to be static, because they survive our stack transitions. */ | |
559 | static int status = 0; | |
560 | - static char *realstack; | |
561 | - char *scratch, *simstack; | |
562 | + static void *realstack; | |
563 | + void *simstack_alloc_base, *simstack; | |
564 | + size_t simstack_size, page_size; | |
565 | int i; | |
566 | ||
567 | auto void doit (void); | |
568 | @@ -145,9 +194,35 @@ | |
569 | } | |
570 | ||
571 | assert (grub_scratch_mem == 0); | |
572 | - scratch = malloc (0x100000 + EXTENDED_MEMSIZE + 15); | |
573 | - assert (scratch); | |
574 | - grub_scratch_mem = (char *) ((((int) scratch) >> 4) << 4); | |
575 | + | |
576 | + /* Allocate enough pages for 0x100000 + EXTENDED_SIZE + 15, and | |
577 | + * make sure the memory is aligned to a multiple of the system's | |
578 | + * page size */ | |
579 | + page_size = sysconf (_SC_PAGESIZE); | |
580 | + simstack_size = ( 0x100000 + EXTENDED_MEMSIZE + 15); | |
581 | + if (simstack_size % page_size) | |
582 | + { | |
583 | + /* If we're not on a page_size boundary, round up to the next one */ | |
584 | + simstack_size &= ~(page_size-1); | |
585 | + simstack_size += page_size; | |
586 | + } | |
587 | + | |
588 | + /* Add one for a PROT_NONE boundary page at each end. */ | |
589 | + simstack_size += 2 * page_size; | |
590 | + | |
591 | + simstack_alloc_base = grub_mmap_alloc(simstack_size); | |
592 | + assert (simstack_alloc_base != MAP_FAILED); | |
593 | + | |
594 | + /* mark pages above and below our simstack area as innaccessable. | |
595 | + * If the implementation we're using doesn't support that, then the | |
596 | + * new protection modes are undefined. It's safe to just ignore | |
597 | + * them, though. It'd be nice if we knew that we'd get a SEGV for | |
598 | + * touching the area, but that's all. it'd be nice to have. */ | |
599 | + mprotect (simstack_alloc_base, page_size, PROT_NONE); | |
600 | + mprotect ((void *)((unsigned long)simstack_alloc_base + | |
601 | + simstack_size - page_size), page_size, PROT_NONE); | |
602 | + | |
603 | + grub_scratch_mem = (void *)((unsigned long)simstack_alloc_base + page_size); | |
604 | ||
605 | /* FIXME: simulate the memory holes using mprot, if available. */ | |
606 | ||
607 | @@ -220,7 +295,7 @@ | |
608 | device_map = 0; | |
609 | free (disks); | |
610 | disks = 0; | |
611 | - free (scratch); | |
612 | + munmap(simstack_alloc_base, simstack_size); | |
613 | grub_scratch_mem = 0; | |
614 | ||
615 | if (serial_device) |