char * grub_util_read_image (const char *path) { char *img; FILE *fp; size_t size; grub_util_info ("reading %s", path); size = grub_util_get_image_size (path); img = (char *) xmalloc (size); fp = fopen (path, "rb"); if (! fp) grub_util_error (_("cannot open `%s': %s"), path, strerror (errno)); if (fread (img, 1, size, fp) != size) grub_util_error (_("cannot read `%s': %s"), path, strerror (errno)); fclose (fp); return img; }
void grub_util_create_envblk_file (const char *name) { FILE *fp; char *buf; char *namenew; buf = xmalloc (DEFAULT_ENVBLK_SIZE); namenew = xasprintf ("%s.new", name); fp = grub_util_fopen (namenew, "wb"); if (! fp) grub_util_error (_("cannot open `%s': %s"), namenew, strerror (errno)); memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1); memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#', DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1); if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE) grub_util_error (_("cannot write to `%s': %s"), namenew, strerror (errno)); fsync (fileno (fp)); free (buf); fclose (fp); if (grub_util_rename (namenew, name) < 0) grub_util_error (_("cannot rename the file %s to %s"), namenew, name); free (namenew); }
static void process_input_dir (const char *input_dir, enum grub_install_plat platform) { char *platsub = grub_install_get_platform_name (platform); char *grubdir = grub_util_path_concat (3, rootdir, subdir, platsub); char *load_cfg = grub_util_path_concat (2, grubdir, "load.cfg"); char *prefix; char *output; char *grub_cfg; FILE *cfg; grub_install_copy_files (input_dir, base, platform); grub_util_unlink (load_cfg); if (debug_image) { FILE *f = grub_util_fopen (load_cfg, "wb"); if (!f) grub_util_error (_("cannot open `%s': %s"), load_cfg, strerror (errno)); fprintf (f, "set debug='%s'\n", debug_image); fclose (f); } else { free (load_cfg); load_cfg = 0; } prefix = xasprintf ("/%s", subdir); if (!targets[platform].mkimage_target) grub_util_error (_("unsupported platform %s"), platsub); grub_cfg = grub_util_path_concat (2, grubdir, "grub.cfg"); cfg = grub_util_fopen (grub_cfg, "wb"); if (!cfg) grub_util_error (_("cannot open `%s': %s"), grub_cfg, strerror (errno)); fprintf (cfg, "source %s/grub.cfg", subdir); fclose (cfg); grub_install_push_module (targets[platform].netmodule); output = grub_util_path_concat_ext (2, grubdir, "core", targets[platform].ext); grub_install_make_image_wrap (input_dir, prefix, output, 0, load_cfg, targets[platform].mkimage_target, 0); grub_install_pop_module (); /* TRANSLATORS: First %s is replaced by platform name. Second one by filename. */ printf (_("Netboot directory for %s created. Configure your DHCP server to point to %s\n"), platsub, output); free (platsub); free (output); free (prefix); free (grub_cfg); free (grubdir); }
void grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out) { grub_util_info ("writing 0x%x bytes at offset 0x%x", size, offset); if (fseeko (out, offset, SEEK_SET) == -1) grub_util_error ("seek failed"); if (fwrite (img, 1, size, out) != size) grub_util_error ("write failed"); }
void grub_util_read_at (void *img, size_t size, off_t offset, FILE *fp) { if (fseeko (fp, offset, SEEK_SET) == -1) grub_util_error ("seek failed"); if (fread (img, 1, size, fp) != size) grub_util_error ("read failed"); }
int main (int argc, char *argv[]) { char *text; struct arguments arguments; grub_util_host_init (&argc, &argv); /* Check for options. */ memset (&arguments, 0, sizeof (struct arguments)); if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0) { fprintf (stderr, "%s", _("Error in parsing command line arguments\n")); exit(1); } if ((!arguments.input && !arguments.text) || !arguments.font) { fprintf (stderr, "%s", _("Missing arguments\n")); exit(1); } if (arguments.text) text = arguments.text; else { FILE *in = grub_util_fopen (arguments.input, "r"); size_t s; if (!in) grub_util_error (_("cannot open `%s': %s"), arguments.input, strerror (errno)); fseek (in, 0, SEEK_END); s = ftell (in); fseek (in, 0, SEEK_SET); text = xmalloc (s + 1); if (fread (text, 1, s, in) != s) grub_util_error (_("cannot read `%s': %s"), arguments.input, strerror (errno)); text[s] = 0; fclose (in); } grub_init_all (); grub_hostfs_init (); grub_host_init (); grub_util_render_label (arguments.font, arguments.bgcolor, arguments.fgcolor, text, arguments.output); return 0; }
void grub_util_write_image_at (const void *img, size_t size, off_t offset, FILE *out, const char *name) { grub_util_info ("writing 0x%" PRIxGRUB_SIZE " bytes at offset 0x%llx", size, (unsigned long long) offset); if (fseeko (out, offset, SEEK_SET) == -1) grub_util_error (_("cannot seek `%s': %s"), name, strerror (errno)); if (fwrite (img, 1, size, out) != size) grub_util_error (_("cannot write to `%s': %s"), name, strerror (errno)); }
size_t grub_util_get_fp_size (FILE *fp) { struct stat st; if (fflush (fp) == EOF) grub_util_error ("fflush failed"); if (fstat (fileno (fp), &st) == -1) grub_util_error ("fstat failed"); return st.st_size; }
void grub_util_write_image (const char *img, size_t size, FILE *out, const char *name) { grub_util_info ("writing 0x%" PRIxGRUB_SIZE " bytes", size); if (fwrite (img, 1, size, out) != size) { if (!name) grub_util_error (_("cannot write to the stdout: %s"), strerror (errno)); else grub_util_error (_("cannot write to `%s': %s"), name, strerror (errno)); } }
void grub_util_write_image (const char *img, size_t size, FILE *out) { grub_util_info ("writing 0x%x bytes", size); if (fwrite (img, 1, size, out) != size) grub_util_error ("write failed"); }
int main (int argc, char **argv) { size_t module_size; unsigned arch; char *module_img; if (argc != 3) { fprintf (stderr, "usage: %s FILE ARCH\n", argv[0]); return 1; } for (arch = 0; arch < ARRAY_SIZE(archs); arch++) if (strcmp(archs[arch].name, argv[2]) == 0) break; if (arch == ARRAY_SIZE(archs)) grub_util_error("unknown arch: %s", argv[2]); module_size = grub_util_get_image_size (argv[1]); module_img = grub_util_read_image (argv[1]); if (archs[arch].voidp_sizeof == 8) grub_module_verify64(module_img, module_size, &archs[arch]); else grub_module_verify32(module_img, module_size, &archs[arch]); return 0; }
void grub_install_register_efi (grub_device_t efidir_grub_dev, const char *efifile_path, const char *efi_distributor) { grub_util_error ("%s", _("no EFI routines are available for your platform")); }
grub_int64_t grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize) { unsigned long long nr; unsigned sector_size, log_sector_size; if (ioctl (fd, DIOCGMEDIASIZE, &nr)) return -1; if (ioctl (fd, DIOCGSECTORSIZE, §or_size)) return -1; if (sector_size & (sector_size - 1) || !sector_size) return -1; for (log_sector_size = 0; (1 << log_sector_size) < sector_size; log_sector_size++); if (log_secsize) *log_secsize = log_sector_size; if (nr & (sector_size - 1)) grub_util_error ("%s", _("unaligned device size")); return nr; }
static const gcry_md_spec_t * grub_md_sha512_real (void) { const gcry_md_spec_t *ret; ret = grub_crypto_lookup_md_by_name ("sha512"); if (!ret) grub_util_error ("%s", _("Couldn't load sha512")); return ret; }
static void block_device_get_sysfs_path_and_link(const char *devicenode, char *sysfs_path, int sysfs_path_len) { char *rpath = xmalloc (PATH_MAX); snprintf(sysfs_path, sysfs_path_len, "/sys/block/%s", devicenode); if (!realpath (sysfs_path, rpath)) grub_util_error ("cannot get the real path of `%s'", sysfs_path); strcat(rpath, "/device"); if (!realpath (rpath, sysfs_path)) grub_util_error ("cannot get the real path of `%s'", rpath); free (rpath); }
void * xrealloc (void *ptr, grub_size_t size) { ptr = realloc (ptr, size); if (! ptr) grub_util_error ("%s", _("out of memory")); return ptr; }
void grub_util_load_image (const char *path, char *buf) { FILE *fp; size_t size; grub_util_info ("reading %s", path); size = grub_util_get_image_size (path); fp = fopen (path, "rb"); if (! fp) grub_util_error ("cannot open %s", path); if (fread (buf, 1, size, fp) != size) grub_util_error ("cannot read %s", path); fclose (fp); }
int main (int argc, char *argv[]) { FILE *in32, *in64, *out; struct arguments arguments; set_program_name (argv[0]); /* Check for options. */ memset (&arguments, 0, sizeof (struct arguments)); if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0) { fprintf (stderr, "%s", _("Error in parsing command line arguments\n")); exit(1); } if (!arguments.input32 || !arguments.input64) { fprintf (stderr, "%s", _("Missing input file\n")); exit(1); } in32 = fopen (arguments.input32, "r"); if (!in32) grub_util_error (_("cannot open `%s': %s"), arguments.input32, strerror (errno)); in64 = fopen (arguments.input64, "r"); if (!in64) grub_util_error (_("cannot open `%s': %s"), arguments.input64, strerror (errno)); if (arguments.output) out = fopen (arguments.output, "wb"); else out = stdout; if (!out) { grub_util_error (_("cannot open `%s': %s"), arguments.output ? : "stdout", strerror (errno)); }
void * xmalloc (grub_size_t size) { void *p; p = malloc (size); if (! p) grub_util_error ("%s", _("out of memory")); return p; }
static grub_err_t execute_command (char *name, int n, char **args) { grub_command_t cmd; cmd = grub_command_find (name); if (! cmd) grub_util_error (_("can\'t find command %s"), name); return (cmd->func) (cmd, n, args); }
size_t grub_util_get_image_size (const char *path) { struct stat st; grub_util_info ("getting the size of %s", path); if (stat (path, &st) == -1) grub_util_error (_("cannot stat `%s': %s"), path, strerror (errno)); return st.st_size; }
char * xasprintf (const char *fmt, ...) { va_list ap; char *result; va_start (ap, fmt); result = grub_xvasprintf (fmt, ap); if (!result) grub_util_error ("%s", _("out of memory")); return result; }
void grub_util_pull_devmapper (const char *os_dev) { struct dm_tree *tree; struct dm_tree_node *node; struct dm_tree_node *child; void *handle = NULL; char *lastsubdev = NULL; char *uuid; uuid = get_dm_uuid (os_dev); if (!grub_util_open_dm (os_dev, &tree, &node)) { grub_free (uuid); return; } while ((child = dm_tree_next_child (&handle, node, 0))) { const struct dm_info *dm = dm_tree_node_get_info (child); char *subdev; if (!dm) continue; subdev = grub_find_device ("/dev", makedev (dm->major, dm->minor)); if (subdev) { lastsubdev = subdev; grub_util_pull_device (subdev); } } if (uuid && strncmp (uuid, "CRYPT-LUKS1-", sizeof ("CRYPT-LUKS1-") - 1) == 0 && lastsubdev) { char *grdev = grub_util_get_grub_dev (lastsubdev); dm_tree_free (tree); if (grdev) { grub_err_t err; err = grub_cryptodisk_cheat_mount (grdev, os_dev); if (err) grub_util_error (_("can't mount encrypted volume `%s': %s"), lastsubdev, grub_errmsg); } grub_free (grdev); } else dm_tree_free (tree); grub_free (uuid); }
static char * xrealpath (const char *in) { char *out; #ifdef PATH_MAX out = xmalloc (PATH_MAX); out = realpath (in, out); #else out = realpath (in, NULL); #endif if (!out) grub_util_error (_("failed to get canonical path of `%s'"), in); return out; }
char * xasprintf (const char *fmt, ...) { va_list ap; char *result; va_start (ap, fmt); if (vasprintf (&result, fmt, ap) < 0) { if (errno == ENOMEM) grub_util_error ("out of memory"); return NULL; } return result; }
/* Convert POSIX path to Win32 path, remove drive letter, replace backslashes. */ static char * get_win32_path (const char *path) { char winpath[PATH_MAX]; if (cygwin_conv_path (CCP_POSIX_TO_WIN_A, path, winpath, sizeof(winpath))) grub_util_error ("cygwin_conv_path() failed"); int len = strlen (winpath); int offs = (len > 2 && winpath[1] == ':' ? 2 : 0); int i; for (i = offs; i < len; i++) if (winpath[i] == '\\') winpath[i] = '/'; return xstrdup (winpath + offs); }
char * grub_util_make_temporary_file (void) { const char *t = getenv ("TMPDIR"); size_t tl; char *tmp; if (!t) t = "/tmp"; tl = strlen (t); tmp = xmalloc (tl + sizeof ("/grub.XXXXXX")); memcpy (tmp, t, tl); memcpy (tmp + tl, "/grub.XXXXXX", sizeof ("/grub.XXXXXX")); if (mkstemp (tmp) == -1) grub_util_error (_("cannot make temporary file: %s"), strerror (errno)); return tmp; }
static void cmd_cp (char *src, char *dest) { FILE *ff; auto int cp_hook (grub_off_t ofs, char *buf, int len); int cp_hook (grub_off_t ofs, char *buf, int len) { (void) ofs; if ((int) fwrite (buf, 1, len, ff) != len) { grub_util_error (_("write error")); return 1; } return 0; }
static grub_uint64_t grub_util_get_fd_size_volume (grub_util_fd_t fd __attribute__ ((unused)), const char *dev, unsigned *log_secsize) { struct DriveGeometry *geo; LONG err; unsigned sector_size, log_sector_size; if (!bounce) bounce = AllocVec (BOUNCE_SIZE, MEMF_PUBLIC | MEMF_CLEAR); if (!bounce) grub_util_error ("out of memory"); fd->ioreq->iotd_Req.io_Command = TD_GETGEOMETRY; fd->ioreq->iotd_Req.io_Length = sizeof (*geo); fd->ioreq->iotd_Req.io_Data = bounce; fd->ioreq->iotd_Req.io_Offset = 0; fd->ioreq->iotd_Req.io_Actual = 0; err = DoIO ((struct IORequest *) fd->ioreq); if (err) { grub_util_info ("I/O failed with error %d, IoErr=%d", (int)err, (int) IoErr ()); return -1; } geo = (struct DriveGeometry *) bounce; sector_size = geo->dg_SectorSize; if (sector_size & (sector_size - 1) || !sector_size) return -1; for (log_sector_size = 0; (1 << log_sector_size) < sector_size; log_sector_size++); if (log_secsize) *log_secsize = log_sector_size; return (grub_uint64_t) geo->dg_TotalSectors * (grub_uint64_t) geo->dg_SectorSize; }
static grub_disk_memberlist_t grub_lvm_memberlist (grub_disk_t disk) { struct grub_lvm_lv *lv = disk->data; grub_disk_memberlist_t list = NULL, tmp; struct grub_lvm_pv *pv; if (lv->vg->pvs) for (pv = lv->vg->pvs; pv; pv = pv->next) { if (!pv->disk) grub_util_error ("Couldn't find PV %s. Check your device.map", pv->name); tmp = grub_malloc (sizeof (*tmp)); tmp->disk = pv->disk; tmp->next = list; list = tmp; } return list; }