1 --- grub-0.97/grub/asmstub.c
2 +++ grub-0.97/grub/asmstub.c
10 # include <sys/ioctl.h> /* ioctl */
12 struct apm_info apm_bios_info;
14 /* Emulation requirements. */
15 -char *grub_scratch_mem = 0;
16 +void *grub_scratch_mem = 0;
18 struct geometry *disks = 0;
21 static unsigned int serial_speed;
22 #endif /* SIMULATE_SLOWNESS_OF_SERIAL */
24 +/* This allocates page-aligned storage of the specified size, which must be
25 + * a multiple of the page size as determined by calling sysconf(_SC_PAGESIZE)
29 +grub_mmap_alloc(size_t len)
31 + int mmap_flags = MAP_ANONYMOUS|MAP_PRIVATE|MAP_EXECUTABLE;
34 + mmap_flags |= MAP_32BIT;
36 + /* Mark the simulated stack executable, as GCC uses stack trampolines
37 + * to implement nested functions. */
38 + return mmap(NULL, len, PROT_READ|PROT_WRITE|PROT_EXEC, mmap_flags, -1, 0);
40 +#else /* !defined(__linux__) */
42 +grub_mmap_alloc(size_t len)
44 + int fd = 0, offset = 0, ret = 0;
45 + void *pa = MAP_FAILED;
46 + char template[] = "/tmp/grub_mmap_alloc_XXXXXX";
49 + fd = mkstemp(template);
55 + ret = ftruncate(fd, len);
59 + /* Mark the simulated stack executable, as GCC uses stack trampolines
60 + * to implement nested functions. */
61 + pa = mmap(NULL, len, PROT_READ|PROT_WRITE|PROT_EXEC,
62 + MAP_PRIVATE|MAP_EXECUTABLE, fd, offset);
69 +#endif /* defined(__linux__) */
71 /* The main entry point into this mess. */
75 /* These need to be static, because they survive our stack transitions. */
76 static int status = 0;
77 - static char *realstack;
78 - char *scratch, *simstack;
79 + static void *realstack;
80 + void *simstack_alloc_base, *simstack;
81 + size_t simstack_size, page_size;
84 /* We need a nested function so that we get a clean stack frame,
88 assert (grub_scratch_mem == 0);
89 - scratch = malloc (0x100000 + EXTENDED_MEMSIZE + 15);
91 - grub_scratch_mem = (char *) ((((int) scratch) >> 4) << 4);
93 + /* Allocate enough pages for 0x100000 + EXTENDED_SIZE + 15, and
94 + * make sure the memory is aligned to a multiple of the system's
96 + page_size = sysconf (_SC_PAGESIZE);
97 + simstack_size = ( 0x100000 + EXTENDED_MEMSIZE + 15);
98 + if (simstack_size % page_size)
100 + /* If we're not on a page_size boundary, round up to the next one */
101 + simstack_size &= ~(page_size-1);
102 + simstack_size += page_size;
105 + /* Add one for a PROT_NONE boundary page at each end. */
106 + simstack_size += 2 * page_size;
108 + simstack_alloc_base = grub_mmap_alloc(simstack_size);
109 + assert (simstack_alloc_base != MAP_FAILED);
111 + /* mark pages above and below our simstack area as innaccessable.
112 + * If the implementation we're using doesn't support that, then the
113 + * new protection modes are undefined. It's safe to just ignore
114 + * them, though. It'd be nice if we knew that we'd get a SEGV for
115 + * touching the area, but that's all. it'd be nice to have. */
116 + mprotect (simstack_alloc_base, page_size, PROT_NONE);
117 + mprotect ((void *)((unsigned long)simstack_alloc_base +
118 + simstack_size - page_size), page_size, PROT_NONE);
120 + grub_scratch_mem = (void *)((unsigned long)simstack_alloc_base + page_size);
122 /* FIXME: simulate the memory holes using mprot, if available. */
129 + munmap(simstack_alloc_base, simstack_size);
130 grub_scratch_mem = 0;
133 --- grub-0.97/stage2/builtins.c
134 +++ grub-0.97/stage2/builtins.c
135 @@ -131,63 +131,98 @@
139 +/* blocklist_read_helper nee disk_read_blocklist_func was a nested
140 + * function, to which pointers were taken and exposed globally. Even
141 + * in the GNU-C nested functions extension, they have local linkage,
142 + * and aren't guaranteed to be accessable *at all* outside of their
143 + * containing scope.
145 + * Above and beyond all of that, the variables within blocklist_func_context
146 + * are originally local variables, with local (not even static) linkage,
147 + * from within blocklist_func. These were each referenced by
148 + * disk_read_blocklist_func, which is only called from other functions
149 + * through a globally scoped pointer.
151 + * The documentation in GCC actually uses the words "all hell will break
152 + * loose" to describe this scenario.
154 + * Also, "start_sector" was also used uninitialized, but gcc doesn't warn
155 + * about it (possibly because of the scoping madness?)
163 +} blocklist_func_context = {
170 +/* Collect contiguous blocks into one entry as many as possible,
171 + and print the blocklist notation on the screen. */
173 +blocklist_read_helper (int sector, int offset, int length)
175 + int *start_sector = &blocklist_func_context.start_sector;
176 + int *num_sectors = &blocklist_func_context.num_sectors;
177 + int *num_entries = &blocklist_func_context.num_entries;
178 + int *last_length = &blocklist_func_context.last_length;
180 + if (*num_sectors > 0)
182 + if (*start_sector + *num_sectors == sector
183 + && offset == 0 && *last_length == SECTOR_SIZE)
186 + *last_length = length;
191 + if (*last_length == SECTOR_SIZE)
192 + grub_printf ("%s%d+%d", *num_entries ? "," : "",
193 + *start_sector - part_start, *num_sectors);
194 + else if (*num_sectors > 1)
195 + grub_printf ("%s%d+%d,%d[0-%d]", *num_entries ? "," : "",
196 + *start_sector - part_start, *num_sectors-1,
197 + *start_sector + *num_sectors-1 - part_start,
200 + grub_printf ("%s%d[0-%d]", *num_entries ? "," : "",
201 + *start_sector - part_start, *last_length);
209 + grub_printf("%s%d[%d-%d]", *num_entries ? "," : "",
210 + sector-part_start, offset, offset+length);
215 + *start_sector = sector;
217 + *last_length = length;
223 blocklist_func (char *arg, int flags)
225 char *dummy = (char *) RAW_ADDR (0x100000);
227 - int num_sectors = 0;
228 - int num_entries = 0;
229 - int last_length = 0;
231 - auto void disk_read_blocklist_func (int sector, int offset, int length);
233 - /* Collect contiguous blocks into one entry as many as possible,
234 - and print the blocklist notation on the screen. */
235 - auto void disk_read_blocklist_func (int sector, int offset, int length)
237 - if (num_sectors > 0)
239 - if (start_sector + num_sectors == sector
240 - && offset == 0 && last_length == SECTOR_SIZE)
243 - last_length = length;
248 - if (last_length == SECTOR_SIZE)
249 - grub_printf ("%s%d+%d", num_entries ? "," : "",
250 - start_sector - part_start, num_sectors);
251 - else if (num_sectors > 1)
252 - grub_printf ("%s%d+%d,%d[0-%d]", num_entries ? "," : "",
253 - start_sector - part_start, num_sectors-1,
254 - start_sector + num_sectors-1 - part_start,
257 - grub_printf ("%s%d[0-%d]", num_entries ? "," : "",
258 - start_sector - part_start, last_length);
266 - grub_printf("%s%d[%d-%d]", num_entries ? "," : "",
267 - sector-part_start, offset, offset+length);
272 - start_sector = sector;
274 - last_length = length;
278 + int *start_sector = &blocklist_func_context.start_sector;
279 + int *num_sectors = &blocklist_func_context.num_sectors;
280 + int *num_entries = &blocklist_func_context.num_entries;
283 if (! grub_open (arg))
285 @@ -204,15 +241,15 @@
288 /* Read in the whole file to DUMMY. */
289 - disk_read_hook = disk_read_blocklist_func;
290 + disk_read_hook = blocklist_read_helper;
291 if (! grub_read (dummy, -1))
294 /* The last entry may not be printed yet. Don't check if it is a
295 * full sector, since it doesn't matter if we read too much. */
296 - if (num_sectors > 0)
297 - grub_printf ("%s%d+%d", num_entries ? "," : "",
298 - start_sector - part_start, num_sectors);
299 + if (*num_sectors > 0)
300 + grub_printf ("%s%d+%d", *num_entries ? "," : "",
301 + *start_sector - part_start, *num_sectors);
305 @@ -1868,6 +1905,77 @@
313 + char *stage2_first_buffer;
314 +} install_func_context = {
318 + .stage2_first_buffer = NULL,
321 +/* Save the first sector of Stage2 in STAGE2_SECT. */
322 +/* Formerly disk_read_savesect_func with local scope inside install_func */
324 +install_savesect_helper(int sector, int offset, int length)
327 + printf ("[%d]", sector);
329 + /* ReiserFS has files which sometimes contain data not aligned
330 + on sector boundaries. Returning an error is better than
331 + silently failing. */
332 + if (offset != 0 || length != SECTOR_SIZE)
333 + errnum = ERR_UNALIGNED;
335 + install_func_context.saved_sector = sector;
338 +/* Write SECTOR to INSTALLLIST, and update INSTALLADDR and INSTALLSECT. */
339 +/* Formerly disk_read_blocklist_func with local scope inside install_func */
341 +install_blocklist_helper (int sector, int offset, int length)
343 + int *installaddr = &install_func_context.installaddr;
344 + int *installlist = &install_func_context.installlist;
345 + char **stage2_first_buffer = &install_func_context.stage2_first_buffer;
346 + /* Was the last sector full? */
347 + static int last_length = SECTOR_SIZE;
350 + printf("[%d]", sector);
352 + if (offset != 0 || last_length != SECTOR_SIZE)
354 + /* We found a non-sector-aligned data block. */
355 + errnum = ERR_UNALIGNED;
359 + last_length = length;
361 + if (*((unsigned long *) (*installlist - 4))
362 + + *((unsigned short *) *installlist) != sector
363 + || *installlist == (int) *stage2_first_buffer + SECTOR_SIZE + 4)
367 + if (*((unsigned long *) (*installlist - 8)))
368 + errnum = ERR_WONT_FIT;
371 + *((unsigned short *) (*installlist + 2)) = (*installaddr >> 4);
372 + *((unsigned long *) (*installlist - 4)) = sector;
376 + *((unsigned short *) *installlist) += 1;
377 + *installaddr += 512;
381 install_func (char *arg, int flags)
383 @@ -1875,8 +1983,12 @@
384 char *stage1_buffer = (char *) RAW_ADDR (0x100000);
385 char *stage2_buffer = stage1_buffer + SECTOR_SIZE;
386 char *old_sect = stage2_buffer + SECTOR_SIZE;
387 - char *stage2_first_buffer = old_sect + SECTOR_SIZE;
388 - char *stage2_second_buffer = stage2_first_buffer + SECTOR_SIZE;
389 + /* stage2_first_buffer used to be defined as:
390 + * char *stage2_first_buffer = old_sect + SECTOR_SIZE; */
391 + char **stage2_first_buffer = &install_func_context.stage2_first_buffer;
392 + /* and stage2_second_buffer was:
393 + * char *stage2_second_buffer = stage2_first_buffer + SECTOR_SIZE; */
394 + char *stage2_second_buffer = old_sect + SECTOR_SIZE + SECTOR_SIZE;
395 /* XXX: Probably SECTOR_SIZE is reasonable. */
396 char *config_filename = stage2_second_buffer + SECTOR_SIZE;
397 char *dummy = config_filename + SECTOR_SIZE;
398 @@ -1885,10 +1997,11 @@
399 int src_drive, src_partition, src_part_start;
401 struct geometry dest_geom, src_geom;
403 + int *saved_sector = &install_func_context.saved_sector;
404 int stage2_first_sector, stage2_second_sector;
406 - int installaddr, installlist;
407 + int *installaddr = &install_func_context.installaddr;
408 + int *installlist = &install_func_context.installlist;
409 /* Point to the location of the name of a configuration file in Stage 2. */
410 char *config_file_location;
411 /* If FILE is a Stage 1.5? */
412 @@ -1897,67 +2010,13 @@
414 /* If LBA is forced? */
415 int is_force_lba = 0;
416 - /* Was the last sector full? */
417 - int last_length = SECTOR_SIZE;
420 + *stage2_first_buffer = old_sect + SECTOR_SIZE;
422 /* If the Stage 2 is in a partition mounted by an OS, this will store
423 the filename under the OS. */
424 char *stage2_os_file = 0;
425 #endif /* GRUB_UTIL */
427 - auto void disk_read_savesect_func (int sector, int offset, int length);
428 - auto void disk_read_blocklist_func (int sector, int offset, int length);
430 - /* Save the first sector of Stage2 in STAGE2_SECT. */
431 - auto void disk_read_savesect_func (int sector, int offset, int length)
434 - printf ("[%d]", sector);
436 - /* ReiserFS has files which sometimes contain data not aligned
437 - on sector boundaries. Returning an error is better than
438 - silently failing. */
439 - if (offset != 0 || length != SECTOR_SIZE)
440 - errnum = ERR_UNALIGNED;
442 - saved_sector = sector;
445 - /* Write SECTOR to INSTALLLIST, and update INSTALLADDR and
447 - auto void disk_read_blocklist_func (int sector, int offset, int length)
450 - printf("[%d]", sector);
452 - if (offset != 0 || last_length != SECTOR_SIZE)
454 - /* We found a non-sector-aligned data block. */
455 - errnum = ERR_UNALIGNED;
459 - last_length = length;
461 - if (*((unsigned long *) (installlist - 4))
462 - + *((unsigned short *) installlist) != sector
463 - || installlist == (int) stage2_first_buffer + SECTOR_SIZE + 4)
467 - if (*((unsigned long *) (installlist - 8)))
468 - errnum = ERR_WONT_FIT;
471 - *((unsigned short *) (installlist + 2)) = (installaddr >> 4);
472 - *((unsigned long *) (installlist - 4)) = sector;
476 - *((unsigned short *) installlist) += 1;
477 - installaddr += 512;
480 /* First, check the GNU-style long option. */
482 @@ -1987,10 +2049,10 @@
483 addr = skip_to (0, file);
485 /* Get the installation address. */
486 - if (! safe_parse_maxint (&addr, &installaddr))
487 + if (! safe_parse_maxint (&addr, installaddr))
489 /* ADDR is not specified. */
495 @@ -2084,17 +2146,17 @@
496 = (dest_drive & BIOS_FLAG_FIXED_DISK);
498 /* Read the first sector of Stage 2. */
499 - disk_read_hook = disk_read_savesect_func;
500 - if (grub_read (stage2_first_buffer, SECTOR_SIZE) != SECTOR_SIZE)
501 + disk_read_hook = install_savesect_helper;
502 + if (grub_read (*stage2_first_buffer, SECTOR_SIZE) != SECTOR_SIZE)
505 - stage2_first_sector = saved_sector;
506 + stage2_first_sector = *saved_sector;
508 /* Read the second sector of Stage 2. */
509 if (grub_read (stage2_second_buffer, SECTOR_SIZE) != SECTOR_SIZE)
512 - stage2_second_sector = saved_sector;
513 + stage2_second_sector = *saved_sector;
515 /* Check for the version of Stage 2. */
516 if (*((short *) (stage2_second_buffer + STAGE2_VER_MAJ_OFFS))
517 @@ -2110,27 +2172,27 @@
519 /* If INSTALLADDR is not specified explicitly in the command-line,
520 determine it by the Stage 2 id. */
522 + if (! *installaddr)
526 - installaddr = 0x8000;
527 + *installaddr = 0x8000;
530 - installaddr = 0x2000;
531 + *installaddr = 0x2000;
534 *((unsigned long *) (stage1_buffer + STAGE1_STAGE2_SECTOR))
535 = stage2_first_sector;
536 *((unsigned short *) (stage1_buffer + STAGE1_STAGE2_ADDRESS))
539 *((unsigned short *) (stage1_buffer + STAGE1_STAGE2_SEGMENT))
540 - = installaddr >> 4;
541 + = *installaddr >> 4;
543 - i = (int) stage2_first_buffer + SECTOR_SIZE - 4;
544 + i = (int) *stage2_first_buffer + SECTOR_SIZE - 4;
545 while (*((unsigned long *) i))
547 - if (i < (int) stage2_first_buffer
548 + if (i < (int) *stage2_first_buffer
549 || (*((int *) (i - 4)) & 0x80000000)
550 || *((unsigned short *) i) >= 0xA00
551 || *((short *) (i + 2)) == 0)
552 @@ -2144,13 +2206,13 @@
556 - installlist = (int) stage2_first_buffer + SECTOR_SIZE + 4;
557 - installaddr += SECTOR_SIZE;
558 + *installlist = (int) *stage2_first_buffer + SECTOR_SIZE + 4;
559 + *installaddr += SECTOR_SIZE;
561 /* Read the whole of Stage2 except for the first sector. */
562 grub_seek (SECTOR_SIZE);
564 - disk_read_hook = disk_read_blocklist_func;
565 + disk_read_hook = install_blocklist_helper;
566 if (! grub_read (dummy, -1))
569 @@ -2233,7 +2295,7 @@
570 /* Skip the first sector. */
571 grub_seek (SECTOR_SIZE);
573 - disk_read_hook = disk_read_savesect_func;
574 + disk_read_hook = install_savesect_helper;
575 if (grub_read (stage2_buffer, SECTOR_SIZE) != SECTOR_SIZE)
578 @@ -2303,7 +2365,7 @@
580 #endif /* GRUB_UTIL */
582 - if (! devwrite (saved_sector - part_start, 1, stage2_buffer))
583 + if (! devwrite (*saved_sector - part_start, 1, stage2_buffer))
587 @@ -2325,7 +2387,7 @@
591 - if (fwrite (stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE)
592 + if (fwrite (*stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE)
596 @@ -2352,7 +2414,7 @@
599 if (! devwrite (stage2_first_sector - src_part_start, 1,
600 - stage2_first_buffer))
601 + *stage2_first_buffer))
604 if (! devwrite (stage2_second_sector - src_part_start, 1,
605 --- grub-0.97/stage2/shared.h
606 +++ grub-0.97/stage2/shared.h
609 /* Maybe redirect memory requests through grub_scratch_mem. */
611 -extern char *grub_scratch_mem;
612 -# define RAW_ADDR(x) ((x) + (int) grub_scratch_mem)
613 +extern void *grub_scratch_mem;
614 +# define RAW_ADDR(x) ((x) + (unsigned long) grub_scratch_mem)
615 # define RAW_SEG(x) (RAW_ADDR ((x) << 4) >> 4)
617 # define RAW_ADDR(x) (x)