]> git.ipfire.org Git - ipfire-2.x.git/blob - src/patches/grub-0.97/grub-0.96-nxstack.patch
HinzugefĆ¼gt:
[ipfire-2.x.git] / src / patches / grub-0.97 / grub-0.96-nxstack.patch
1 --- grub-0.97/grub/asmstub.c
2 +++ grub-0.97/grub/asmstub.c
3 @@ -42,6 +42,7 @@
4 #include <sys/time.h>
5 #include <termios.h>
6 #include <signal.h>
7 +#include <sys/mman.h>
8
9 #ifdef __linux__
10 # include <sys/ioctl.h> /* ioctl */
11 @@ -79,7 +80,7 @@
12 struct apm_info apm_bios_info;
13
14 /* Emulation requirements. */
15 -char *grub_scratch_mem = 0;
16 +void *grub_scratch_mem = 0;
17
18 struct geometry *disks = 0;
19
20 @@ -103,14 +104,62 @@
21 static unsigned int serial_speed;
22 #endif /* SIMULATE_SLOWNESS_OF_SERIAL */
23
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)
26 + */
27 +#ifdef __linux__
28 +static void *
29 +grub_mmap_alloc(size_t len)
30 +{
31 + int mmap_flags = MAP_ANONYMOUS|MAP_PRIVATE|MAP_EXECUTABLE;
32 +
33 +#ifdef MAP_32BIT
34 + mmap_flags |= MAP_32BIT;
35 +#endif
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);
39 +}
40 +#else /* !defined(__linux__) */
41 +static void *
42 +grub_mmap_alloc(size_t len)
43 +{
44 + int fd = 0, offset = 0, ret = 0;
45 + void *pa = MAP_FAILED;
46 + char template[] = "/tmp/grub_mmap_alloc_XXXXXX";
47 + errno_t e;
48 +
49 + fd = mkstemp(template);
50 + if (fd < 0)
51 + return pa;
52 +
53 + unlink(template);
54 +
55 + ret = ftruncate(fd, len);
56 + if (ret < 0)
57 + return pa;
58 +
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);
63 +
64 + e = errno;
65 + close(fd);
66 + errno = e;
67 + return pa;
68 +}
69 +#endif /* defined(__linux__) */
70 +
71 /* The main entry point into this mess. */
72 int
73 grub_stage2 (void)
74 {
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;
82 int i;
83
84 /* We need a nested function so that we get a clean stack frame,
85 @@ -140,9 +189,35 @@
86 }
87
88 assert (grub_scratch_mem == 0);
89 - scratch = malloc (0x100000 + EXTENDED_MEMSIZE + 15);
90 - assert (scratch);
91 - grub_scratch_mem = (char *) ((((int) scratch) >> 4) << 4);
92 +
93 + /* Allocate enough pages for 0x100000 + EXTENDED_SIZE + 15, and
94 + * make sure the memory is aligned to a multiple of the system's
95 + * page size */
96 + page_size = sysconf (_SC_PAGESIZE);
97 + simstack_size = ( 0x100000 + EXTENDED_MEMSIZE + 15);
98 + if (simstack_size % page_size)
99 + {
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;
103 + }
104 +
105 + /* Add one for a PROT_NONE boundary page at each end. */
106 + simstack_size += 2 * page_size;
107 +
108 + simstack_alloc_base = grub_mmap_alloc(simstack_size);
109 + assert (simstack_alloc_base != MAP_FAILED);
110 +
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);
119 +
120 + grub_scratch_mem = (void *)((unsigned long)simstack_alloc_base + page_size);
121
122 /* FIXME: simulate the memory holes using mprot, if available. */
123
124 @@ -215,7 +290,7 @@
125 device_map = 0;
126 free (disks);
127 disks = 0;
128 - free (scratch);
129 + munmap(simstack_alloc_base, simstack_size);
130 grub_scratch_mem = 0;
131
132 if (serial_device)
133 --- grub-0.97/stage2/builtins.c
134 +++ grub-0.97/stage2/builtins.c
135 @@ -131,63 +131,98 @@
136 }
137
138 \f
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.
144 + *
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.
150 + *
151 + * The documentation in GCC actually uses the words "all hell will break
152 + * loose" to describe this scenario.
153 + *
154 + * Also, "start_sector" was also used uninitialized, but gcc doesn't warn
155 + * about it (possibly because of the scoping madness?)
156 + */
157 +
158 +static struct {
159 + int start_sector;
160 + int num_sectors;
161 + int num_entries;
162 + int last_length;
163 +} blocklist_func_context = {
164 + .start_sector = 0,
165 + .num_sectors = 0,
166 + .num_entries = 0,
167 + .last_length = 0
168 +};
169 +
170 +/* Collect contiguous blocks into one entry as many as possible,
171 + and print the blocklist notation on the screen. */
172 +static void
173 +blocklist_read_helper (int sector, int offset, int length)
174 +{
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;
179 +
180 + if (*num_sectors > 0)
181 + {
182 + if (*start_sector + *num_sectors == sector
183 + && offset == 0 && *last_length == SECTOR_SIZE)
184 + {
185 + *num_sectors++;
186 + *last_length = length;
187 + return;
188 + }
189 + else
190 + {
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,
198 + *last_length);
199 + else
200 + grub_printf ("%s%d[0-%d]", *num_entries ? "," : "",
201 + *start_sector - part_start, *last_length);
202 + *num_entries++;
203 + *num_sectors = 0;
204 + }
205 + }
206 +
207 + if (offset > 0)
208 + {
209 + grub_printf("%s%d[%d-%d]", *num_entries ? "," : "",
210 + sector-part_start, offset, offset+length);
211 + *num_entries++;
212 + }
213 + else
214 + {
215 + *start_sector = sector;
216 + *num_sectors = 1;
217 + *last_length = length;
218 + }
219 +}
220 +
221 /* blocklist */
222 static int
223 blocklist_func (char *arg, int flags)
224 {
225 char *dummy = (char *) RAW_ADDR (0x100000);
226 - int start_sector;
227 - int num_sectors = 0;
228 - int num_entries = 0;
229 - int last_length = 0;
230 -
231 - auto void disk_read_blocklist_func (int sector, int offset, int length);
232 -
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)
236 - {
237 - if (num_sectors > 0)
238 - {
239 - if (start_sector + num_sectors == sector
240 - && offset == 0 && last_length == SECTOR_SIZE)
241 - {
242 - num_sectors++;
243 - last_length = length;
244 - return;
245 - }
246 - else
247 - {
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,
255 - last_length);
256 - else
257 - grub_printf ("%s%d[0-%d]", num_entries ? "," : "",
258 - start_sector - part_start, last_length);
259 - num_entries++;
260 - num_sectors = 0;
261 - }
262 - }
263 -
264 - if (offset > 0)
265 - {
266 - grub_printf("%s%d[%d-%d]", num_entries ? "," : "",
267 - sector-part_start, offset, offset+length);
268 - num_entries++;
269 - }
270 - else
271 - {
272 - start_sector = sector;
273 - num_sectors = 1;
274 - last_length = length;
275 - }
276 - }
277
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;
281 +
282 /* Open the file. */
283 if (! grub_open (arg))
284 return 1;
285 @@ -204,15 +241,15 @@
286 grub_printf (")");
287
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))
292 goto fail;
293
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);
302
303 grub_printf ("\n");
304
305 @@ -1868,6 +1905,77 @@
306
307 \f
308 /* install */
309 +static struct {
310 + int saved_sector;
311 + int installaddr;
312 + int installlist;
313 + char *stage2_first_buffer;
314 +} install_func_context = {
315 + .saved_sector = 0,
316 + .installaddr = 0,
317 + .installlist = 0,
318 + .stage2_first_buffer = NULL,
319 +};
320 +
321 +/* Save the first sector of Stage2 in STAGE2_SECT. */
322 +/* Formerly disk_read_savesect_func with local scope inside install_func */
323 +static void
324 +install_savesect_helper(int sector, int offset, int length)
325 +{
326 + if (debug)
327 + printf ("[%d]", sector);
328 +
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;
334 +
335 + install_func_context.saved_sector = sector;
336 +}
337 +
338 +/* Write SECTOR to INSTALLLIST, and update INSTALLADDR and INSTALLSECT. */
339 +/* Formerly disk_read_blocklist_func with local scope inside install_func */
340 +static void
341 +install_blocklist_helper (int sector, int offset, int length)
342 +{
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;
348 +
349 + if (debug)
350 + printf("[%d]", sector);
351 +
352 + if (offset != 0 || last_length != SECTOR_SIZE)
353 + {
354 + /* We found a non-sector-aligned data block. */
355 + errnum = ERR_UNALIGNED;
356 + return;
357 + }
358 +
359 + last_length = length;
360 +
361 + if (*((unsigned long *) (*installlist - 4))
362 + + *((unsigned short *) *installlist) != sector
363 + || *installlist == (int) *stage2_first_buffer + SECTOR_SIZE + 4)
364 + {
365 + *installlist -= 8;
366 +
367 + if (*((unsigned long *) (*installlist - 8)))
368 + errnum = ERR_WONT_FIT;
369 + else
370 + {
371 + *((unsigned short *) (*installlist + 2)) = (*installaddr >> 4);
372 + *((unsigned long *) (*installlist - 4)) = sector;
373 + }
374 + }
375 +
376 + *((unsigned short *) *installlist) += 1;
377 + *installaddr += 512;
378 +}
379 +
380 static int
381 install_func (char *arg, int flags)
382 {
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;
400 int i;
401 struct geometry dest_geom, src_geom;
402 - int saved_sector;
403 + int *saved_sector = &install_func_context.saved_sector;
404 int stage2_first_sector, stage2_second_sector;
405 char *ptr;
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 @@
413 int is_open = 0;
414 /* If LBA is forced? */
415 int is_force_lba = 0;
416 - /* Was the last sector full? */
417 - int last_length = SECTOR_SIZE;
418 -
419 +
420 + *stage2_first_buffer = old_sect + SECTOR_SIZE;
421 #ifdef GRUB_UTIL
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 */
426 -
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);
429 -
430 - /* Save the first sector of Stage2 in STAGE2_SECT. */
431 - auto void disk_read_savesect_func (int sector, int offset, int length)
432 - {
433 - if (debug)
434 - printf ("[%d]", sector);
435 -
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;
441 -
442 - saved_sector = sector;
443 - }
444 -
445 - /* Write SECTOR to INSTALLLIST, and update INSTALLADDR and
446 - INSTALLSECT. */
447 - auto void disk_read_blocklist_func (int sector, int offset, int length)
448 - {
449 - if (debug)
450 - printf("[%d]", sector);
451 -
452 - if (offset != 0 || last_length != SECTOR_SIZE)
453 - {
454 - /* We found a non-sector-aligned data block. */
455 - errnum = ERR_UNALIGNED;
456 - return;
457 - }
458 -
459 - last_length = length;
460 -
461 - if (*((unsigned long *) (installlist - 4))
462 - + *((unsigned short *) installlist) != sector
463 - || installlist == (int) stage2_first_buffer + SECTOR_SIZE + 4)
464 - {
465 - installlist -= 8;
466 -
467 - if (*((unsigned long *) (installlist - 8)))
468 - errnum = ERR_WONT_FIT;
469 - else
470 - {
471 - *((unsigned short *) (installlist + 2)) = (installaddr >> 4);
472 - *((unsigned long *) (installlist - 4)) = sector;
473 - }
474 - }
475 -
476 - *((unsigned short *) installlist) += 1;
477 - installaddr += 512;
478 - }
479
480 /* First, check the GNU-style long option. */
481 while (1)
482 @@ -1987,10 +2049,10 @@
483 addr = skip_to (0, file);
484
485 /* Get the installation address. */
486 - if (! safe_parse_maxint (&addr, &installaddr))
487 + if (! safe_parse_maxint (&addr, installaddr))
488 {
489 /* ADDR is not specified. */
490 - installaddr = 0;
491 + *installaddr = 0;
492 ptr = addr;
493 errnum = 0;
494 }
495 @@ -2084,17 +2146,17 @@
496 = (dest_drive & BIOS_FLAG_FIXED_DISK);
497
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)
503 goto fail;
504
505 - stage2_first_sector = saved_sector;
506 + stage2_first_sector = *saved_sector;
507
508 /* Read the second sector of Stage 2. */
509 if (grub_read (stage2_second_buffer, SECTOR_SIZE) != SECTOR_SIZE)
510 goto fail;
511
512 - stage2_second_sector = saved_sector;
513 + stage2_second_sector = *saved_sector;
514
515 /* Check for the version of Stage 2. */
516 if (*((short *) (stage2_second_buffer + STAGE2_VER_MAJ_OFFS))
517 @@ -2110,27 +2172,27 @@
518
519 /* If INSTALLADDR is not specified explicitly in the command-line,
520 determine it by the Stage 2 id. */
521 - if (! installaddr)
522 + if (! *installaddr)
523 {
524 if (! is_stage1_5)
525 /* Stage 2. */
526 - installaddr = 0x8000;
527 + *installaddr = 0x8000;
528 else
529 /* Stage 1.5. */
530 - installaddr = 0x2000;
531 + *installaddr = 0x2000;
532 }
533
534 *((unsigned long *) (stage1_buffer + STAGE1_STAGE2_SECTOR))
535 = stage2_first_sector;
536 *((unsigned short *) (stage1_buffer + STAGE1_STAGE2_ADDRESS))
537 - = installaddr;
538 + = *installaddr;
539 *((unsigned short *) (stage1_buffer + STAGE1_STAGE2_SEGMENT))
540 - = installaddr >> 4;
541 + = *installaddr >> 4;
542
543 - i = (int) stage2_first_buffer + SECTOR_SIZE - 4;
544 + i = (int) *stage2_first_buffer + SECTOR_SIZE - 4;
545 while (*((unsigned long *) i))
546 {
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 @@
553 i -= 8;
554 }
555
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;
560
561 /* Read the whole of Stage2 except for the first sector. */
562 grub_seek (SECTOR_SIZE);
563
564 - disk_read_hook = disk_read_blocklist_func;
565 + disk_read_hook = install_blocklist_helper;
566 if (! grub_read (dummy, -1))
567 goto fail;
568
569 @@ -2233,7 +2295,7 @@
570 /* Skip the first sector. */
571 grub_seek (SECTOR_SIZE);
572
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)
576 goto fail;
577
578 @@ -2303,7 +2365,7 @@
579 else
580 #endif /* GRUB_UTIL */
581 {
582 - if (! devwrite (saved_sector - part_start, 1, stage2_buffer))
583 + if (! devwrite (*saved_sector - part_start, 1, stage2_buffer))
584 goto fail;
585 }
586 }
587 @@ -2325,7 +2387,7 @@
588 goto fail;
589 }
590
591 - if (fwrite (stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE)
592 + if (fwrite (*stage2_first_buffer, 1, SECTOR_SIZE, fp) != SECTOR_SIZE)
593 {
594 fclose (fp);
595 errnum = ERR_WRITE;
596 @@ -2352,7 +2414,7 @@
597 goto fail;
598
599 if (! devwrite (stage2_first_sector - src_part_start, 1,
600 - stage2_first_buffer))
601 + *stage2_first_buffer))
602 goto fail;
603
604 if (! devwrite (stage2_second_sector - src_part_start, 1,
605 --- grub-0.97/stage2/shared.h
606 +++ grub-0.97/stage2/shared.h
607 @@ -36,8 +36,8 @@
608
609 /* Maybe redirect memory requests through grub_scratch_mem. */
610 #ifdef GRUB_UTIL
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)
616 #else
617 # define RAW_ADDR(x) (x)