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_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); }
/* ZFS has similar problems to those of btrfs (see above). */ void grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs) { char *slash; *poolname = *poolfs = NULL; #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && defined(HAVE_STRUCT_STATFS_F_MNTFROMNAME) /* FreeBSD and GNU/kFreeBSD. */ { struct statfs mnt; if (statfs (dir, &mnt) != 0) return; if (strcmp (mnt.f_fstypename, "zfs") != 0) return; *poolname = xstrdup (mnt.f_mntfromname); } #elif defined(HAVE_GETEXTMNTENT) /* Solaris. */ { struct stat st; struct extmnttab mnt; if (stat (dir, &st) != 0) return; FILE *mnttab = grub_util_fopen ("/etc/mnttab", "r"); if (! mnttab) return; while (getextmntent (mnttab, &mnt, sizeof (mnt)) == 0) { if (makedev (mnt.mnt_major, mnt.mnt_minor) == st.st_dev && !strcmp (mnt.mnt_fstype, "zfs")) { *poolname = xstrdup (mnt.mnt_special); break; } } fclose (mnttab); } #endif if (! *poolname) return; slash = strchr (*poolname, '/'); if (slash) { *slash = '\0'; *poolfs = xstrdup (slash + 1); } else *poolfs = xstrdup (""); }
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; }
int grub_get_random (void *out, grub_size_t len) { FILE *f; size_t rd; f = grub_util_fopen ("/dev/urandom", "rb"); if (!f) return 1; rd = fread (out, 1, len, f); fclose (f); if (rd != len) return 1; return 0; }
int main (int argc, char **argv) { FILE *f; size_t len; unsigned char *buf; if (argc < 2) printf ("Usage: %s FILE\n", argv[0]); f = grub_util_fopen (argv[1], "rb"); if (!f) { printf ("Couldn't open file\n"); return 1; } fseek (f, 0, SEEK_END); len = ftell (f); fseek (f, 0, SEEK_SET); buf = malloc (len); if (!buf) { printf (_("error: %s.\n"), _("out of memory")); fclose (f); return 2; } if (fread (buf, 1, len, f) != len) { printf (_("cannot read `%s': %s"), argv[1], strerror (errno)); free (buf); fclose (f); return 2; } printf ("Sleep type = %d\n", get_sleep_type (buf, NULL, buf + len, NULL, 0)); free (buf); fclose (f); return 0; }
static int read_platform_size (void) { FILE *fp; char *buf = NULL; size_t len = 0; int ret = 0; /* Newer kernels can tell us directly about the size of the * underlying firmware - let's see if that interface is there. */ fp = grub_util_fopen ("/sys/firmware/efi/fw_platform_size", "r"); if (fp != NULL) { if (getline (&buf, &len, fp) >= 3) /* 2 digits plus newline */ { if (strncmp (buf, "32", 2) == 0) ret = 32; else if (strncmp (buf, "64", 2) == 0) ret = 64; } free (buf); fclose (fp); } if (ret == 0) { /* Unrecognised - fall back to matching the kernel size * instead */ if (is_64_kernel ()) ret = 64; else ret = 32; } return ret; }
static void read_device_map (const char *dev_map) { FILE *fp; char buf[1024]; /* XXX */ int lineno = 0; if (dev_map[0] == '\0') { grub_util_info ("no device.map"); return; } fp = grub_util_fopen (dev_map, "r"); if (! fp) { grub_util_info (_("cannot open `%s': %s"), dev_map, strerror (errno)); return; } while (fgets (buf, sizeof (buf), fp)) { char *p = buf; char *e; char *drive_e, *drive_p; int drive; lineno++; /* Skip leading spaces. */ while (*p && grub_isspace (*p)) p++; /* If the first character is `#' or NUL, skip this line. */ if (*p == '\0' || *p == '#') continue; if (*p != '(') { char *tmp; tmp = xasprintf (_("missing `%c' symbol"), '('); grub_util_error ("%s:%d: %s", dev_map, lineno, tmp); } p++; /* Find a free slot. */ drive = find_free_slot (); if (drive < 0) grub_util_error ("%s:%d: %s", dev_map, lineno, _("device count exceeds limit")); e = p; p = strchr (p, ')'); if (! p) { char *tmp; tmp = xasprintf (_("missing `%c' symbol"), ')'); grub_util_error ("%s:%d: %s", dev_map, lineno, tmp); } map[drive].drive = 0; if ((e[0] == 'f' || e[0] == 'h' || e[0] == 'c') && e[1] == 'd') { char *ptr; for (ptr = e + 2; ptr < p; ptr++) if (!grub_isdigit (*ptr)) break; if (ptr == p) { map[drive].drive = xmalloc (p - e + sizeof ('\0')); strncpy (map[drive].drive, e, p - e + sizeof ('\0')); map[drive].drive[p - e] = '\0'; } if (*ptr == ',') { *p = 0; /* TRANSLATORS: Only one entry is ignored. However the suggestion is to correct/delete the whole file. device.map is a file indicating which devices are available at boot time. Fedora populated it with entries like (hd0,1) /dev/sda1 which would mean that every partition is a separate disk for BIOS. Such entries were inactive in GRUB due to its bug which is now gone. Without this additional check these entries would be harmful now. */ grub_util_warn (_("the device.map entry `%s' is invalid. " "Ignoring it. Please correct or " "delete your device.map"), e); continue; } } drive_e = e; drive_p = p; map[drive].device_map = 1; p++; /* Skip leading spaces. */ while (*p && grub_isspace (*p)) p++; if (*p == '\0') grub_util_error ("%s:%d: %s", dev_map, lineno, _("filename expected")); /* NUL-terminate the filename. */ e = p; while (*e && ! grub_isspace (*e)) e++; *e = '\0'; if (!grub_util_check_file_presence (p)) { free (map[drive].drive); map[drive].drive = NULL; grub_util_info ("Cannot stat `%s', skipping", p); continue; } /* On Linux, the devfs uses symbolic links horribly, and that confuses the interface very much, so use realpath to expand symbolic links. */ map[drive].device = canonicalize_file_name (p); if (! map[drive].device) map[drive].device = xstrdup (p); if (!map[drive].drive) { char c; map[drive].drive = xmalloc (sizeof ("hostdisk/") + strlen (p)); memcpy (map[drive].drive, "hostdisk/", sizeof ("hostdisk/") - 1); strcpy (map[drive].drive + sizeof ("hostdisk/") - 1, p); c = *drive_p; *drive_p = 0; /* TRANSLATORS: device.map is a filename. Not to be translated. device.map specifies disk correspondance overrides. Previously one could create any kind of device name with this. Due to some problems we decided to limit it to just a handful possibilities. */ grub_util_warn (_("the drive name `%s' in device.map is incorrect. " "Using %s instead. " "Please use the form [hfc]d[0-9]* " "(E.g. `hd0' or `cd')"), drive_e, map[drive].drive); *drive_p = c; } grub_util_info ("adding `%s' -> `%s' from device.map", map[drive].drive, map[drive].device); grub_hostdisk_flush_initial_buffer (map[drive].device); } fclose (fp); }