void grub_install_get_blocklist (grub_device_t root_dev, const char *core_path, const char *core_img, size_t core_size, void (*callback) (grub_disk_addr_t sector, unsigned offset, unsigned length, void *data), void *hook_data) { int i; char *tmp_img; char *core_path_dev; core_path_dev = grub_make_system_path_relative_to_its_root (core_path); /* Make sure that GRUB reads the identical image as the OS. */ tmp_img = xmalloc (core_size); for (i = 0; i < MAX_TRIES; i++) { grub_file_t file; grub_util_info ((i == 0) ? _("attempting to read the core image `%s' from GRUB") : _("attempting to read the core image `%s' from GRUB again"), core_path_dev); grub_disk_cache_invalidate_all (); grub_file_filter_disable_compression (); file = grub_file_open (core_path_dev); if (file) { if (grub_file_size (file) != core_size) grub_util_info ("succeeded in opening the core image but the size is different (%d != %d)", (int) grub_file_size (file), (int) core_size); else if (grub_file_read (file, tmp_img, core_size) != (grub_ssize_t) core_size) grub_util_info ("succeeded in opening the core image but cannot read %d bytes", (int) core_size); else if (memcmp (core_img, tmp_img, core_size) != 0) { #if 0 FILE *dump; FILE *dump2; dump = fopen ("dump.img", "wb"); if (dump) { fwrite (tmp_img, 1, core_size, dump); fclose (dump); } dump2 = fopen ("dump2.img", "wb"); if (dump2) { fwrite (core_img, 1, core_size, dump2); fclose (dump2); } #endif grub_util_info ("succeeded in opening the core image but the data is different"); } else { grub_file_close (file); break; } grub_file_close (file); } else grub_util_info ("couldn't open the core image"); if (grub_errno) grub_util_info ("error message = %s", grub_errmsg); grub_errno = GRUB_ERR_NONE; grub_util_biosdisk_flush (root_dev->disk); sleep (1); } if (i == MAX_TRIES) grub_util_error (_("cannot read `%s' correctly"), core_path_dev); grub_file_t file; /* Now read the core image to determine where the sectors are. */ grub_file_filter_disable_compression (); file = grub_file_open (core_path_dev); if (! file) grub_util_error ("%s", grub_errmsg); file->read_hook = callback; file->read_hook_data = hook_data; if (grub_file_read (file, tmp_img, core_size) != (grub_ssize_t) core_size) grub_util_error ("%s", _("failed to read the sectors of the core image")); grub_file_close (file); free (tmp_img); free (core_path_dev); }
int main (int argc, char *argv[]) { char *root_dev = 0; char *dir = DEFAULT_DIRECTORY; char *dev_map = DEFAULT_DEVICE_MAP; volatile int hold = 0; int opt; set_program_name (argv[0]); while ((opt = getopt_long (argc, argv, "r:d:m:vH:hV", options, 0)) != -1) switch (opt) { case 'r': root_dev = optarg; break; case 'd': dir = optarg; break; case 'm': dev_map = optarg; break; case 'v': verbosity++; break; case 'H': hold = (optarg ? atoi (optarg) : -1); break; case 'h': return usage (0); case 'V': printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION); return 0; default: return usage (1); } if (optind < argc) { fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind]); return usage (1); } /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */ if (hold && verbosity > 0) printf ("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n", program_name, (int) getpid ()); while (hold) { if (hold > 0) hold--; sleep (1); } signal (SIGINT, SIG_IGN); grub_console_init (); grub_host_init (); grub_hostfs_init (); /* XXX: This is a bit unportable. */ grub_util_biosdisk_init (dev_map); #if GRUB_NO_MODULES grub_init_all (); #endif /* Make sure that there is a root device. */ if (! root_dev) { char *device_name = grub_guess_root_device (dir); if (! device_name) grub_util_error ("cannot find a device for %s", dir); root_dev = grub_util_get_grub_dev (device_name); if (! root_dev) { grub_util_info ("guessing the root device failed, because of `%s'", grub_errmsg); grub_util_error ("cannot guess the root device. Specify the option `--root-device'"); } } if (strcmp (root_dev, "host") == 0) dir = xstrdup (dir); else dir = grub_make_system_path_relative_to_its_root (dir); prefix = xmalloc (strlen (root_dev) + 2 + strlen (dir) + 1); sprintf (prefix, "(%s)%s", root_dev, dir); free (dir); /* Start GRUB! */ if (setjmp (main_env) == 0) grub_main (); #if GRUB_NO_MODULES grub_fini_all (); #endif grub_hostfs_fini (); grub_host_fini (); grub_machine_fini (); return 0; }