static int load_file(char *name, void **ptr, long *size) { char *buf = NULL; int allocated = 1 * 1024 * 1024; int len, filled = 0; if (!grub_open (name)) return -1; buf = malloc(allocated); errnum = 0; while (1) { len = grub_read (buf + filled, allocated - filled); if (! len) { if (!errnum) break; grub_close (); return -1; } filled += len; if (filled < allocated) break; allocated *= 2; buf = realloc(buf, allocated); } grub_close (); *ptr = buf; *size = filled; return 0; }
void cmain (void) { grub_printf ("\n\nGRUB loading, please wait...\n"); /* * Here load the true second-stage boot-loader. */ if (grub_open (config_file)) { int ret; disk_read_hook = disk_read_savesect_func; grub_read ((char *) 0x8000, SECTOR_SIZE * 2); disk_read_hook = NULL; /* Sanity check: catch an internal error. */ if (saved_sector == -1) { grub_printf ("internal error: the second sector of Stage 2 is unknown."); stop (); } ret = grub_read ((char *) 0x8000 + SECTOR_SIZE * 2, -1); grub_close (); if (ret) chain_stage2 (0, 0x8200, saved_sector); } /* * If not, then print error message and die. */ print_error (); stop (); }