Imported Upstream version 0.5.4
[debian/efibootmgr] / src / efibootmgr / efibootmgr.c
index 1aa3595e38253686e430c4f6c7ac86df1679b033..5db0d9e65e1304d17dbeef09e6fe6631614f69f7 100644 (file)
@@ -32,6 +32,7 @@
 
 #define _GNU_SOURCE
 
+#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -95,6 +96,18 @@ fill_var(efi_variable_t *var, const char *name)
                | EFI_VARIABLE_RUNTIME_ACCESS;
 }
 
+static void
+free_vars(list_t *head)
+{
+       list_t *pos, *n;
+       var_entry_t *boot;
+
+       list_for_each_safe(pos, n, head) {
+               boot = list_entry(pos, var_entry_t, list);
+               list_del(&(boot->list));
+               free(boot);
+       }
+}
 
 static void
 read_vars(struct dirent **namelist,
@@ -215,8 +228,8 @@ warn_duplicate_name(list_t *boot_list)
                        boot->var_data.Data;
                if (!efichar_char_strcmp(opts.label,
                                         load_option->description)) {
-                       fprintf(stderr, "** Warning ** : Boot%04x has same label %s\n",
-                              boot->num,
+                       fprintf(stderr, "** Warning ** : %.8s has same label %s\n",
+                              boot->name->d_name,
                               opts.label);
                }
        }
@@ -228,9 +241,21 @@ make_boot_var(list_t *boot_list)
 {
        var_entry_t *boot;
        int free_number;
+       list_t *pos;
 
-       if (opts.bootnum == -1) free_number = find_free_boot_var(boot_list);
-       else                    free_number = opts.bootnum;
+       if (opts.bootnum == -1)
+               free_number = find_free_boot_var(boot_list);
+       else {
+               list_for_each(pos, boot_list) {
+                       boot = list_entry(pos, var_entry_t, list);
+                       if (boot->num == opts.bootnum) {
+                               fprintf(stderr, "** Warning ** : bootnum %04X "
+                                       "already exists\n", opts.bootnum);
+                               return NULL;
+                       }
+               }
+               free_number = opts.bootnum;
+       }
 
        if (free_number == -1) return NULL;
 
@@ -398,13 +423,23 @@ delete_boot_var(uint16_t num)
        list_t *pos, *n;
        var_entry_t *boot;
 
-       snprintf(name, sizeof(name), "Boot%04x", num);
+       snprintf(name, sizeof(name), "Boot%04X", num);
        memset(&var, 0, sizeof(var));
        fill_var(&var, name);
        status = delete_variable(&var);
 
-       if (status) return status;
+       /* For backwards compatibility, try to delete abcdef entries as well */
+       if (status) {
+               snprintf(name, sizeof(name), "Boot%04x", num);
+               memset(&var, 0, sizeof(var));
+               fill_var(&var, name);
+               status = delete_variable(&var);
+       }
 
+       if (status) {
+               fprintf (stderr,"\nboot entry: %X not found\n\n",num);
+               return status;
+       }
        list_for_each_safe(pos, n, &boot_entry_list) {
                boot = list_entry(pos, var_entry_t, list);
                if (boot->num == num) {
@@ -424,11 +459,27 @@ set_var_nums(const char *pattern, list_t *list)
        list_t *pos;
        var_entry_t *var;
        int num=0, rc;
+       char *name;
+       int warn=0;
 
        list_for_each(pos, list) {
                var = list_entry(pos, var_entry_t, list);
                rc = sscanf(var->name->d_name, pattern, &num);
-               if (rc == 1) var->num = num;
+               if (rc == 1) {
+                       var->num = num;
+                       name = var->name->d_name; /* shorter name */
+                       if ((isalpha(name[4]) && islower(name[4])) ||
+                           (isalpha(name[5]) && islower(name[5])) ||
+                           (isalpha(name[6]) && islower(name[6])) ||
+                           (isalpha(name[7]) && islower(name[7]))) {
+                               fprintf(stderr, "** Warning ** : %.8s is not "
+                                       "EFI 1.10 compliant (lowercase hex in name)\n", name);
+                               warn++;
+                       }
+               }
+       }
+       if (warn) {
+               fprintf(stderr, "** Warning ** : please recreate these using efibootmgr to remove this warning.\n");
        }
 }
 
@@ -528,13 +579,28 @@ unparse_boot_order(uint16_t *order, int length)
        int i;
        printf("BootOrder: ");
        for (i=0; i<length; i++) {
-               printf("%04x", order[i]);
+               printf("%04X", order[i]);
                if (i < (length-1))
                        printf(",");
        }
        printf("\n");
 }
 
+static int
+is_current_boot_entry(int b)
+{
+       list_t *pos;
+       var_entry_t *boot;
+
+       list_for_each(pos, &boot_entry_list) {
+               boot = list_entry(pos, var_entry_t, list);
+               if (boot->num == b)
+                       return 1;
+       }
+       return 0;
+}
+
+
 static int
 parse_boot_order(char *buffer, uint16_t *order, int length)
 {
@@ -544,6 +610,16 @@ parse_boot_order(char *buffer, uint16_t *order, int length)
        for (i=0; i<length && *buffer; i++) {
                rc = sscanf(buffer, "%x", &num);
                if (rc == 1) order[i] = num & 0xFFFF;
+               else {
+                       fprintf(stderr,"\nInvalid hex characters in boot order: %s\n\n",buffer);
+                       return -1;
+               }
+               /* make sure this is an existing boot entry */
+               if (!is_current_boot_entry(order[i])) {
+                       fprintf (stderr,"\nboot entry %X does not exist\n\n",order[i]);
+                       return -1;
+               }
+
                /* Advance to the comma */ 
                while (*buffer && *buffer != ',') buffer++;
                /* Advance through the comma(s) */
@@ -564,7 +640,10 @@ set_boot_order()
        fill_var(&boot_order, "BootOrder");
 
        boot_order.DataSize = parse_boot_order(opts.bootorder, n, 1024/sizeof(uint16_t)) * sizeof(uint16_t);
-       return create_or_edit_variable(&boot_order);
+       if (boot_order.DataSize < 0)
+               return 1;
+       else
+               return create_or_edit_variable(&boot_order);
 }
 
 static void
@@ -586,7 +665,11 @@ show_boot_vars()
                                load_option->description, sizeof(description));
                memset(text_path, 0, sizeof(text_path));
                path = load_option_path(load_option);
-               printf("Boot%04x", boot->num);
+               if (boot->name)
+                       printf("%.8s", boot->name->d_name);
+               else
+                       printf("Boot%04X", boot->num);
+
                if (load_option->attributes & LOAD_OPTION_ACTIVE)
                        printf("* ");
                else    printf("  ");
@@ -672,7 +755,9 @@ set_active_state()
                        }
                }
        }
-       return EFI_SUCCESS;
+       /* if we reach here then the bootnumber supplied was not found */
+       fprintf(stderr,"\nboot entry %x not found\n\n",opts.bootnum);
+       return EFI_NOT_FOUND;
 }
 
 
@@ -710,6 +795,7 @@ usage()
        printf("\t-v | --verbose          print additional information\n");
        printf("\t-V | --version          return version and exit\n");
        printf("\t-w | --write-signature  write unique sig to MBR if needed\n");
+       printf("\t-@ | --append-binary-args file  append extra args from file (use \"-\" for stdin)\n");
 }
 
 static void
@@ -769,17 +855,21 @@ parse_opts(int argc, char **argv)
                        {"verbose",          optional_argument, 0, 'v'},
                        {"version",                no_argument, 0, 'V'},
                        {"write-signature",        no_argument, 0, 'w'},
+                       {"append-binary-args", required_argument, 0, '@'},
                        {0, 0, 0, 0}
                };
 
                c = getopt_long (argc, argv,
-                                "AaBb:cd:e:E:gH:i:l:L:n:No:Op:qt:TuU:v::Vw",
+                                "AaBb:cd:e:E:gH:i:l:L:n:No:Op:qt:TuU:v::Vw@:",
                                 long_options, &option_index);
                if (c == -1)
                        break;
 
                switch (c)
                {
+               case '@':
+                       opts.extra_opts_file = optarg;
+                       break;
                case 'a':
                        opts.active = 1;
                        break;
@@ -790,8 +880,12 @@ parse_opts(int argc, char **argv)
                        opts.delete_boot = 1;
                        break;
                case 'b':
-                       rc = sscanf(optarg, "%x", &num);
+                       rc = sscanf(optarg, "%X", &num);
                        if (rc == 1) opts.bootnum = num;
+                       else {
+                               fprintf (stderr,"invalid hex value %s\n",optarg);
+                               exit(1);
+                       }
                        break;
                case 'c':
                        opts.create = 1;
@@ -802,10 +896,18 @@ parse_opts(int argc, char **argv)
                case 'e':
                        rc = sscanf(optarg, "%d", &num);
                        if (rc == 1) opts.edd_version = num;
+                       else {
+                               fprintf (stderr,"invalid numeric value %s\n",optarg);
+                               exit(1);
+                       }
                        break;
                case 'E':
                        rc = sscanf(optarg, "%x", &num);
                        if (rc == 1) opts.edd10_devicenum = num;
+                       else {
+                               fprintf (stderr,"invalid hex value %s\n",optarg);
+                               exit(1);
+                       }
                        break;
                case 'g':
                        opts.forcegpt = 1;
@@ -813,6 +915,10 @@ parse_opts(int argc, char **argv)
                case 'H':
                        rc = sscanf(optarg, "%x", &num);
                        if (rc == 1) opts.acpi_hid = num;
+                       else {
+                               fprintf (stderr,"invalid hex value %s\n",optarg);
+                               exit(1);
+                       }
                        break;
                case 'i':
                        opts.iface = optarg;
@@ -829,6 +935,10 @@ parse_opts(int argc, char **argv)
                case 'n':
                        rc = sscanf(optarg, "%x", &num);
                        if (rc == 1) opts.bootnext = num;
+                       else {
+                               fprintf (stderr,"invalid hex value %s\n",optarg);
+                               exit(1);
+                       }
                        break;
                case 'o':
                        opts.bootorder = optarg;
@@ -839,6 +949,10 @@ parse_opts(int argc, char **argv)
                case 'p':
                        rc = sscanf(optarg, "%u", &num);
                        if (rc == 1) opts.part = num;
+                       else {
+                               fprintf (stderr,"invalid numeric value %s\n",optarg);
+                               exit(1);
+                       }
                        break;
                case 'q':
                        opts.quiet = 1;
@@ -852,6 +966,10 @@ parse_opts(int argc, char **argv)
                                opts.timeout = num;
                                opts.set_timeout = 1;
                        }
+                       else {
+                               fprintf (stderr,"invalid numeric value %s\n",optarg);
+                               exit(1);
+                       }
                        break;
                case 'T':
                        opts.delete_timeout = 1;
@@ -863,6 +981,10 @@ parse_opts(int argc, char **argv)
                case 'U':
                        rc = sscanf(optarg, "%x", &num);
                        if (rc == 1) opts.acpi_uid = num;
+                       else {
+                               fprintf (stderr,"invalid hex value %s\n",optarg);
+                               exit(1);
+                       }
                        break;
                case 'v':
                        opts.verbose = 1;
@@ -871,6 +993,10 @@ parse_opts(int argc, char **argv)
                                if (!strcmp(optarg, "vv")) opts.verbose = 3;
                                rc = sscanf(optarg, "%d", &num);
                                if (rc == 1)  opts.verbose = num;
+                               else {
+                                       fprintf (stderr,"invalid numeric value %s\n",optarg);
+                                       exit(1);
+                               }
                        }
                        break;
                case 'V':
@@ -901,6 +1027,7 @@ main(int argc, char **argv)
        struct dirent  **boot_names = NULL;
        var_entry_t *new_boot = NULL;
        int num, num_boot_names=0;
+       efi_status_t ret=0;
 
        set_default_opts();
        parse_opts(argc, argv);
@@ -920,63 +1047,78 @@ main(int argc, char **argv)
        if (!opts.testfile) {
                num_boot_names = read_boot_var_names(&boot_names);
                read_vars(boot_names, num_boot_names, &boot_entry_list);
-               set_var_nums("Boot%04x-%*s", &boot_entry_list);
+               set_var_nums("Boot%04X-%*s", &boot_entry_list);
 
                if (opts.delete_boot) {
-                       if (opts.bootnum == -1)
+                       if (opts.bootnum == -1) {
                                fprintf(stderr, "\nYou must specify a boot entry to delete (see the -b option).\n\n");
+                               return 1;
+                       }
                        else
-                               delete_boot_var(opts.bootnum);
+                               ret = delete_boot_var(opts.bootnum);
                }
 
                if (opts.active >= 0) {
-                       set_active_state();
+                       if (opts.bootnum == -1) {
+                               fprintf(stderr, "\nYou must specify a boot entry to delete (see the -b option).\n\n");
+                               return 1;
+                       }
+                       else
+                               ret=set_active_state();
                }
        }
 
        if (opts.create) {
                warn_duplicate_name(&boot_entry_list);
                new_boot = make_boot_var(&boot_entry_list);
+               if (!new_boot)
+                       return 1;
+
                /* Put this boot var in the right BootOrder */
                if (!opts.testfile && new_boot)
-                       add_to_boot_order(new_boot->num);
+                       ret=add_to_boot_order(new_boot->num);
        }
 
        if (!opts.testfile) {
 
                if (opts.delete_bootorder) {
-                       delete_var("BootOrder");
+                       ret=delete_var("BootOrder");
                }
 
                if (opts.bootorder) {
-                       set_boot_order();
+                       ret=set_boot_order();
                }
 
 
                if (opts.delete_bootnext) {
-                       delete_var("BootNext");
+                       ret=delete_var("BootNext");
                }
 
                if (opts.delete_timeout) {
-                       delete_var("Timeout");
+                       ret=delete_var("Timeout");
                }
 
                if (opts.bootnext >= 0) {
-                       set_boot_u16("BootNext", opts.bootnext & 0xFFFF);
+                       if (!is_current_boot_entry(opts.bootnext & 0xFFFF)){
+                               fprintf (stderr,"\n\nboot entry %X does not exist\n\n",
+                                       opts.bootnext);
+                               return 1;
+                       }
+                       ret=set_boot_u16("BootNext", opts.bootnext & 0xFFFF);
                }
 
                if (opts.set_timeout) {
-                       set_boot_u16("Timeout", opts.timeout);
+                       ret=set_boot_u16("Timeout", opts.timeout);
                }
 
-               if (!opts.quiet) {
+               if (!opts.quiet && ret == 0) {
                        num = read_boot_u16("BootNext");
                        if (num != -1 ) {
-                               printf("BootNext: %04x\n", num);
+                               printf("BootNext: %04X\n", num);
                        }
                        num = read_boot_u16("BootCurrent");
                        if (num != -1) {
-                               printf("BootCurrent: %04x\n", num);
+                               printf("BootCurrent: %04X\n", num);
                        }
                        num = read_boot_u16("Timeout");
                        if (num != -1) {
@@ -987,6 +1129,9 @@ main(int argc, char **argv)
                }
        }
        free_dirents(boot_names, num_boot_names);
+       free_vars(&boot_entry_list);
+       if (ret)
+               return 1;
        return 0;
 }