Dash:
[t2.git] / package / boot / grub / grub-0.97-nx1-stack.patch
blob92742cd5719c63d14d601b666512bb09e9d60e7e
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 @@
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 @@
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 + */
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 +};
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)
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;
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 + }
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 + }
102 /* blocklist */
103 static int
104 blocklist_func (char *arg, int flags)
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;
112 - auto void disk_read_blocklist_func (int sector, int offset, int length);
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)
118 - if (num_sectors > 0)
120 - if (start_sector + num_sectors == sector
121 - && offset == 0 && last_length == SECTOR_SIZE)
123 - num_sectors++;
124 - last_length = length;
125 - return;
127 - else
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;
145 - if (offset > 0)
147 - grub_printf("%s%d[%d-%d]", num_entries ? "," : "",
148 - sector-part_start, offset, offset+length);
149 - num_entries++;
151 - else
153 - start_sector = sector;
154 - num_sectors = 1;
155 - last_length = length;
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;
162 /* Open the file. */
163 if (! grub_open (arg))
164 @@ -206,15 +241,15 @@
165 grub_printf (")");
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;
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);
182 grub_printf ("\n");
184 @@ -1889,6 +1924,77 @@
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,
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)
205 + if (debug)
206 + printf ("[%d]", sector);
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;
214 + install_func_context.saved_sector = sector;
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)
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;
228 + if (debug)
229 + printf("[%d]", sector);
231 + if (offset != 0 || last_length != SECTOR_SIZE)
233 + /* We found a non-sector-aligned data block. */
234 + errnum = ERR_UNALIGNED;
235 + return;
238 + last_length = length;
240 + if (*((unsigned long *) (*installlist - 4))
241 + + *((unsigned short *) *installlist) != sector
242 + || *installlist == (int) *stage2_first_buffer + SECTOR_SIZE + 4)
244 + *installlist -= 8;
246 + if (*((unsigned long *) (*installlist - 8)))
247 + errnum = ERR_WONT_FIT;
248 + else
250 + *((unsigned short *) (*installlist + 2)) = (*installaddr >> 4);
251 + *((unsigned long *) (*installlist - 4)) = sector;
255 + *((unsigned short *) *installlist) += 1;
256 + *installaddr += 512;
259 static int
260 install_func (char *arg, int flags)
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;
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 */
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);
308 - /* Save the first sector of Stage2 in STAGE2_SECT. */
309 - auto void disk_read_savesect_func (int sector, int offset, int length)
311 - if (debug)
312 - printf ("[%d]", sector);
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;
320 - saved_sector = sector;
323 - /* Write SECTOR to INSTALLLIST, and update INSTALLADDR and
324 - INSTALLSECT. */
325 - auto void disk_read_blocklist_func (int sector, int offset, int length)
327 - if (debug)
328 - printf("[%d]", sector);
330 - if (offset != 0 || last_length != SECTOR_SIZE)
332 - /* We found a non-sector-aligned data block. */
333 - errnum = ERR_UNALIGNED;
334 - return;
337 - last_length = length;
339 - if (*((unsigned long *) (installlist - 4))
340 - + *((unsigned short *) installlist) != sector
341 - || installlist == (int) stage2_first_buffer + SECTOR_SIZE + 4)
343 - installlist -= 8;
345 - if (*((unsigned long *) (installlist - 8)))
346 - errnum = ERR_WONT_FIT;
347 - else
349 - *((unsigned short *) (installlist + 2)) = (installaddr >> 4);
350 - *((unsigned long *) (installlist - 4)) = sector;
354 - *((unsigned short *) installlist) += 1;
355 - installaddr += 512;
358 /* First, check the GNU-style long option. */
359 while (1)
361 @@ -2011,10 +2068,10 @@
362 addr = skip_to (0, file);
364 /* Get the installation address. */
365 - if (! safe_parse_maxint (&addr, &installaddr))
366 + if (! safe_parse_maxint (&addr, installaddr))
368 /* ADDR is not specified. */
369 - installaddr = 0;
370 + *installaddr = 0;
371 ptr = addr;
372 errnum = 0;
374 @@ -2110,17 +2167,17 @@
375 = 0x9090;
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;
384 - stage2_first_sector = saved_sector;
385 + stage2_first_sector = *saved_sector;
387 /* Read the second sector of Stage 2. */
388 if (grub_read (stage2_second_buffer, SECTOR_SIZE) != SECTOR_SIZE)
389 goto fail;
391 - stage2_second_sector = saved_sector;
392 + stage2_second_sector = *saved_sector;
394 /* Check for the version of Stage 2. */
395 if (*((short *) (stage2_second_buffer + STAGE2_VER_MAJ_OFFS))
396 @@ -2136,27 +2193,27 @@
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)
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;
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;
422 - i = (int) stage2_first_buffer + SECTOR_SIZE - 4;
423 + i = (int) *stage2_first_buffer + SECTOR_SIZE - 4;
424 while (*((unsigned long *) i))
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;
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;
440 /* Read the whole of Stage2 except for the first sector. */
441 grub_seek (SECTOR_SIZE);
443 - disk_read_hook = disk_read_blocklist_func;
444 + disk_read_hook = install_blocklist_helper;
445 if (! grub_read (dummy, -1))
446 goto fail;
448 @@ -2259,7 +2316,7 @@
449 /* Skip the first sector. */
450 grub_seek (SECTOR_SIZE);
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;
457 @@ -2329,7 +2386,7 @@
458 else
459 #endif /* GRUB_UTIL */
461 - if (! devwrite (saved_sector - part_start, 1, stage2_buffer))
462 + if (! devwrite (*saved_sector - part_start, 1, stage2_buffer))
463 goto fail;
466 @@ -2351,7 +2408,7 @@
467 goto fail;
470 - if (fwrite (stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE)
471 + if (fwrite (*stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE)
473 fclose (fp);
474 errnum = ERR_WRITE;
475 @@ -2378,7 +2435,7 @@
476 goto fail;
478 if (! devwrite (stage2_first_sector - src_part_start, 1,
479 - stage2_first_buffer))
480 + *stage2_first_buffer))
481 goto fail;
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>
492 #ifdef __linux__
493 # include <sys/ioctl.h> /* ioctl */
494 @@ -82,7 +83,7 @@
495 struct apm_info apm_bios_info;
497 /* Emulation requirements. */
498 -char *grub_scratch_mem = 0;
499 +void *grub_scratch_mem = 0;
501 struct geometry *disks = 0;
503 @@ -106,14 +107,62 @@
504 static unsigned int serial_speed;
505 #endif /* SIMULATE_SLOWNESS_OF_SERIAL */
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)
514 + int mmap_flags = MAP_ANONYMOUS|MAP_PRIVATE|MAP_EXECUTABLE;
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);
523 +#else /* !defined(__linux__) */
524 +static void *
525 +grub_mmap_alloc(size_t len)
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;
532 + fd = mkstemp(template);
533 + if (fd < 0)
534 + return pa;
536 + unlink(template);
538 + ret = ftruncate(fd, len);
539 + if (ret < 0)
540 + return pa;
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);
547 + e = errno;
548 + close(fd);
549 + errno = e;
550 + return pa;
552 +#endif /* defined(__linux__) */
554 /* The main entry point into this mess. */
556 grub_stage2 (void)
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;
567 auto void doit (void);
568 @@ -145,9 +194,35 @@
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);
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)
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;
588 + /* Add one for a PROT_NONE boundary page at each end. */
589 + simstack_size += 2 * page_size;
591 + simstack_alloc_base = grub_mmap_alloc(simstack_size);
592 + assert (simstack_alloc_base != MAP_FAILED);
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);
603 + grub_scratch_mem = (void *)((unsigned long)simstack_alloc_base + page_size);
605 /* FIXME: simulate the memory holes using mprot, if available. */
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;
615 if (serial_device)