|
| 1 | +#include <efiboot.h> |
| 2 | +#include <jansson.h> |
| 3 | + |
| 4 | +#include "parse_loader_data.h" |
| 5 | +#include "efibootmgr.h" |
| 6 | +#include "error.h" |
| 7 | +#include "json.h" |
| 8 | + |
| 9 | +static void |
| 10 | +json_fill_bootnext(json_t *root) |
| 11 | +{ |
| 12 | + char s[5] = {}; |
| 13 | + json_t *value; |
| 14 | + int num; |
| 15 | + |
| 16 | + num = read_u16("BootNext"); |
| 17 | + cond_warning(opts.verbose >= 2 && num < 0, |
| 18 | + "Could not read variable 'BootNext'"); |
| 19 | + if (num >= 0) { |
| 20 | + snprintf(s, sizeof(s), "%04X", num); |
| 21 | + value = json_string(s); |
| 22 | + json_object_set_new(root, "BootNext", value); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +static void |
| 27 | +json_fill_bootcurrent(json_t *root) |
| 28 | +{ |
| 29 | + char s[5] = {}; |
| 30 | + json_t *value; |
| 31 | + int num; |
| 32 | + |
| 33 | + num = read_u16("BootCurrent"); |
| 34 | + cond_warning(opts.verbose >= 2 && num < 0, |
| 35 | + "Could not read variable 'BootCurrent'"); |
| 36 | + if (num >= 0) { |
| 37 | + snprintf(s, sizeof(s), "%04X", num); |
| 38 | + value = json_string(s); |
| 39 | + json_object_set_new(root, "BootCurrent", value); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +static void |
| 44 | +json_fill_timeout(json_t *root) |
| 45 | +{ |
| 46 | + json_t *value; |
| 47 | + int num; |
| 48 | + |
| 49 | + num = read_u16("Timeout"); |
| 50 | + cond_warning(opts.verbose >= 2 && num < 0, |
| 51 | + "Could not read variable 'Timeout'"); |
| 52 | + if (num >= 0) { |
| 53 | + value = json_integer(num); |
| 54 | + json_object_set_new(root, "Timeout", value); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +static json_t * |
| 59 | +bootorder_json_array(uint16_t *order, int length) |
| 60 | +{ |
| 61 | + json_t *value, *array; |
| 62 | + char s[5] = {}; |
| 63 | + int i; |
| 64 | + |
| 65 | + array = json_array(); |
| 66 | + if (!array) |
| 67 | + return NULL; |
| 68 | + |
| 69 | + for (i = 0; i < length; i++) { |
| 70 | + snprintf(s, sizeof(s), "%04X", order[i]); |
| 71 | + value = json_string(s); |
| 72 | + json_array_append_new(array, value); |
| 73 | + } |
| 74 | + |
| 75 | + return array; |
| 76 | +} |
| 77 | + |
| 78 | +static void |
| 79 | +json_fill_order(json_t *root, const char *name) |
| 80 | +{ |
| 81 | + var_entry_t *order = NULL; |
| 82 | + uint16_t *data; |
| 83 | + json_t *array; |
| 84 | + int rc; |
| 85 | + |
| 86 | + rc = read_order(name, &order); |
| 87 | + cond_warning(opts.verbose >= 2 && rc < 0, |
| 88 | + "Could not read variable '%s'", name); |
| 89 | + |
| 90 | + if (rc < 0) { |
| 91 | + if (errno == ENOENT) { |
| 92 | + if (!strcmp(name, "BootOrder")) |
| 93 | + printf("No BootOrder is set; firmware will attempt recovery\n"); |
| 94 | + else |
| 95 | + printf("No %s is set\n", name); |
| 96 | + } else |
| 97 | + perror("json_fill_order()"); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + data = (uint16_t *)order->data; |
| 102 | + if (order->data_size) { |
| 103 | + array = bootorder_json_array(data, |
| 104 | + order->data_size / sizeof(uint16_t)); |
| 105 | + if (array != NULL) |
| 106 | + json_object_set_new(root, name, array); |
| 107 | + free(order->data); |
| 108 | + } |
| 109 | + free(order); |
| 110 | +} |
| 111 | + |
| 112 | +static void |
| 113 | +json_fill_vars(json_t *root, const char *prefix, list_t *entry_list) |
| 114 | +{ |
| 115 | + const unsigned char *description; |
| 116 | + json_t *boot_json, *vars_json; |
| 117 | + efi_load_option *load_option; |
| 118 | + char name[16] = {'\0'}; |
| 119 | + var_entry_t *boot; |
| 120 | + list_t *pos; |
| 121 | + int active; |
| 122 | + |
| 123 | + vars_json = json_array(); |
| 124 | + if (!vars_json) |
| 125 | + return; |
| 126 | + |
| 127 | + list_for_each(pos, entry_list) { |
| 128 | + boot_json = json_object(); |
| 129 | + boot = list_entry(pos, var_entry_t, list); |
| 130 | + load_option = (efi_load_option *)boot->data; |
| 131 | + description = efi_loadopt_desc(load_option, boot->data_size); |
| 132 | + if (boot->name) |
| 133 | + json_object_set_new(boot_json, "name", json_string(boot->name)); |
| 134 | + else { |
| 135 | + snprintf(name, sizeof(name), "%s%04X", prefix, boot->num); |
| 136 | + json_object_set_new(boot_json, "name", json_string(boot->name)); |
| 137 | + } |
| 138 | + |
| 139 | + active = efi_loadopt_attrs(load_option) & LOAD_OPTION_ACTIVE ? 1 : 0; |
| 140 | + json_object_set_new(boot_json, "active", json_boolean(active)); |
| 141 | + json_object_set_new(boot_json, "description", |
| 142 | + json_string((char *)description)); |
| 143 | + json_array_append_new(vars_json, boot_json); |
| 144 | + } |
| 145 | + |
| 146 | + json_object_set_new(root, "vars", vars_json); |
| 147 | +} |
| 148 | + |
| 149 | +void |
| 150 | +__print_json(list_t *entry_list, ebm_mode mode, char **prefices, char **order_name) |
| 151 | +{ |
| 152 | + json_t *root = json_object(); |
| 153 | + char *json_str = NULL; |
| 154 | + |
| 155 | + switch (mode) { |
| 156 | + case boot: |
| 157 | + json_fill_bootnext(root); |
| 158 | + json_fill_bootcurrent(root); |
| 159 | + json_fill_timeout(root); |
| 160 | + json_fill_order(root, order_name[mode]); |
| 161 | + json_fill_vars(root, prefices[mode], entry_list); |
| 162 | + break; |
| 163 | + case driver: |
| 164 | + case sysprep: |
| 165 | + json_fill_order(root, order_name[mode]); |
| 166 | + json_fill_vars(root, prefices[mode], entry_list); |
| 167 | + break; |
| 168 | + } |
| 169 | + json_str = json_dumps(root, JSON_COMPACT); |
| 170 | + printf("%s\n", json_str); |
| 171 | + free(json_str); |
| 172 | + json_decref(root); |
| 173 | +} |
0 commit comments