📄 grub-setup.c
字号:
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; sync (); sleep (1); } if (i == MAX_TRIES) grub_util_error ("Cannot read `%s' correctly", core_path); /* Clean out the blocklists. */ block = first_block; while (block->len) { block->start = 0; block->len = 0; block->segment = 0; block--; if ((char *) block <= core_img) grub_util_error ("No terminator in the core image"); } /* Now read the core image to determine where the sectors are. */ file = grub_file_open (core_path); if (! file) grub_util_error ("%s", grub_errmsg); file->read_hook = save_first_sector; if (grub_file_read (file, tmp_img, GRUB_DISK_SECTOR_SIZE) != GRUB_DISK_SECTOR_SIZE) grub_util_error ("Failed to read the first sector of the core image"); block = first_block; file->read_hook = save_blocklists; if (grub_file_read (file, tmp_img, core_size - GRUB_DISK_SECTOR_SIZE) != (grub_ssize_t) core_size - GRUB_DISK_SECTOR_SIZE) grub_util_error ("Failed to read the rest sectors of the core image"); free (core_path); free (tmp_img); *kernel_sector = grub_cpu_to_le32 (first_sector); /* If the destination device is different from the root device, it is necessary to embed the boot drive explicitly. */ if (root_dev->disk->id != dest_dev->disk->id) *boot_drive = (grub_uint8_t) root_dev->disk->id; else *boot_drive = 0xFF; /* Embed information about the installed location. */ if (root_dev->disk->partition) { struct grub_pc_partition *pcdata = root_dev->disk->partition->data; if (strcmp (root_dev->disk->partition->partmap->name, "pc_partition_map") != 0) grub_util_error ("No PC style partitions found"); *install_dos_part = grub_cpu_to_le32 (pcdata->dos_part); *install_bsd_part = grub_cpu_to_le32 (pcdata->bsd_part); } else *install_dos_part = *install_bsd_part = grub_cpu_to_le32 (-1); grub_util_info ("dos partition is %u, bsd partition is %u, prefix is %s", grub_le_to_cpu32 (*install_dos_part), grub_le_to_cpu32 (*install_bsd_part), prefix); strcpy (install_prefix, prefix); /* Write the first two sectors of the core image onto the disk. */ core_path = grub_util_get_path (dir, core_file); grub_util_info ("opening the core image `%s'", core_path); fp = fopen (core_path, "r+b"); if (! fp) grub_util_error ("Cannot open `%s'", core_path); grub_util_write_image (core_img, GRUB_DISK_SECTOR_SIZE * 2, fp); fclose (fp); free (core_path); /* Write the boot image onto the disk. */ if (grub_disk_write (dest_dev->disk, 0, 0, GRUB_DISK_SECTOR_SIZE, boot_img)) grub_util_error ("%s", grub_errmsg); finish: /* Sync is a Good Thing. */ sync (); free (core_img); free (boot_img); grub_device_close (dest_dev); grub_device_close (root_dev);}static struct option options[] = { {"boot-image", required_argument, 0, 'b'}, {"core-image", required_argument, 0, 'c'}, {"directory", required_argument, 0, 'd'}, {"device-map", required_argument, 0, 'm'}, {"root-device", required_argument, 0, 'r'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0} };static voidusage (int status){ if (status) fprintf (stderr, "Try ``grub-setup --help'' for more information.\n"); else printf ("\Usage: grub-setup [OPTION]... DEVICE\n\\n\Set up images to boot from DEVICE.\n\DEVICE must be a GRUB device (e.g. ``(hd0,0)'').\n\\n\ -b, --boot-image=FILE use FILE as the boot image [default=%s]\n\ -c, --core-image=FILE use FILE as the core image [default=%s]\n\ -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n\ -m, --device-map=FILE use FILE as the device map [default=%s]\n\ -r, --root-device=DEV use DEV as the root device [default=guessed]\n\ -h, --help display this message and exit\n\ -V, --version print version information and exit\n\ -v, --verbose print verbose messages\n\\n\Report bugs to <%s>.\n\", DEFAULT_BOOT_FILE, DEFAULT_CORE_FILE, DEFAULT_DIRECTORY, DEFAULT_DEVICE_MAP, PACKAGE_BUGREPORT); exit (status);}static char *get_device_name (char *dev){ size_t len = strlen (dev); if (dev[0] != '(' || dev[len - 1] != ')') return 0; dev[len - 1] = '\0'; return dev + 1;}intmain (int argc, char *argv[]){ char *boot_file = 0; char *core_file = 0; char *dir = 0; char *dev_map = 0; char *root_dev = 0; char *prefix; char *dest_dev; progname = "grub-setup"; /* Check for options. */ while (1) { int c = getopt_long (argc, argv, "b:c:d:m:r:hVv", options, 0); if (c == -1) break; else switch (c) { case 'b': if (boot_file) free (boot_file); boot_file = xstrdup (optarg); break; case 'c': if (core_file) free (core_file); core_file = xstrdup (optarg); break; case 'd': if (dir) free (dir); dir = xstrdup (optarg); break; case 'm': if (dev_map) free (dev_map); dev_map = xstrdup (optarg); break; case 'r': if (root_dev) free (root_dev); root_dev = xstrdup (optarg); break; case 'h': usage (0); break; case 'V': printf ("grub-setup (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION); return 0; case 'v': verbosity++; break; default: usage (1); break; } } /* Obtain DEST_DEV. */ if (optind >= argc) { fprintf (stderr, "No device is specified.\n"); usage (1); } if (optind + 1 != argc) { fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind + 1]); usage (1); } /* Initialize the emulated biosdisk driver. */ grub_util_biosdisk_init (dev_map ? : DEFAULT_DEVICE_MAP); grub_pc_partition_map_init (); dest_dev = get_device_name (argv[optind]); if (! dest_dev) { /* Possibly, the user specified an OS device file. */ dest_dev = grub_util_biosdisk_get_grub_dev (argv[optind]); if (! dest_dev) { fprintf (stderr, "Invalid device `%s'.\n", argv[optind]); usage (1); } } else /* For simplicity. */ dest_dev = xstrdup (dest_dev); prefix = grub_get_prefix (dir ? : DEFAULT_DIRECTORY); /* Initialize filesystems. */ grub_fat_init (); grub_ext2_init (); grub_ufs_init (); grub_minix_init (); grub_hfs_init (); grub_jfs_init (); if (root_dev) { char *tmp = get_device_name (root_dev); if (! tmp) grub_util_error ("Invalid root device `%s'", root_dev); tmp = xstrdup (tmp); free (root_dev); root_dev = tmp; } else { root_dev = grub_guess_root_device (dir ? : DEFAULT_DIRECTORY); 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''."); } } /* Do the real work. */ setup (prefix, dir ? : DEFAULT_DIRECTORY, boot_file ? : DEFAULT_BOOT_FILE, core_file ? : DEFAULT_CORE_FILE, root_dev, dest_dev); /* Free resources. */ grub_ext2_fini (); grub_fat_fini (); grub_ufs_fini (); grub_minix_fini (); grub_hfs_fini (); grub_jfs_fini (); grub_pc_partition_map_fini (); grub_util_biosdisk_fini (); free (boot_file); free (core_file); free (dir); free (dev_map); free (root_dev); free (prefix); free (dest_dev); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -