|
| 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_path(json_t *root, efi_load_option *load_option, |
| 114 | + size_t boot_data_size) |
| 115 | +{ |
| 116 | + size_t text_path_len = 0; |
| 117 | + char *text_path = NULL; |
| 118 | + uint16_t pathlen; |
| 119 | + efidp dp = NULL; |
| 120 | + ssize_t rc; |
| 121 | + |
| 122 | + pathlen = efi_loadopt_pathlen(load_option, boot_data_size); |
| 123 | + dp = efi_loadopt_path(load_option, boot_data_size); |
| 124 | + |
| 125 | + rc = efidp_format_device_path(NULL, 0, dp, pathlen); |
| 126 | + if (rc < 0) |
| 127 | + return; |
| 128 | + |
| 129 | + text_path_len = rc + 1; |
| 130 | + |
| 131 | + text_path = calloc(1, text_path_len); |
| 132 | + if (!text_path) |
| 133 | + return; |
| 134 | + |
| 135 | + rc = efidp_format_device_path((unsigned char *)text_path, |
| 136 | + text_path_len, |
| 137 | + dp, |
| 138 | + pathlen); |
| 139 | + if (rc < 0) { |
| 140 | + free(text_path); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + json_object_set_new(root, "device_path", json_string(text_path)); |
| 145 | + |
| 146 | + free(text_path); |
| 147 | +} |
| 148 | + |
| 149 | +static void |
| 150 | +json_fill_vars(json_t *root, const char *prefix, list_t *entry_list) |
| 151 | +{ |
| 152 | + const unsigned char *description; |
| 153 | + json_t *boot_json, *vars_json; |
| 154 | + efi_load_option *load_option; |
| 155 | + char name[16] = {'\0'}; |
| 156 | + var_entry_t *boot; |
| 157 | + list_t *pos; |
| 158 | + int active; |
| 159 | + |
| 160 | + vars_json = json_array(); |
| 161 | + if (!vars_json) |
| 162 | + return; |
| 163 | + |
| 164 | + list_for_each(pos, entry_list) { |
| 165 | + boot_json = json_object(); |
| 166 | + boot = list_entry(pos, var_entry_t, list); |
| 167 | + load_option = (efi_load_option *)boot->data; |
| 168 | + description = efi_loadopt_desc(load_option, boot->data_size); |
| 169 | + if (boot->name) |
| 170 | + json_object_set_new(boot_json, "name", json_string(boot->name)); |
| 171 | + else { |
| 172 | + snprintf(name, sizeof(name), "%s%04X", prefix, boot->num); |
| 173 | + json_object_set_new(boot_json, "name", json_string(name)); |
| 174 | + } |
| 175 | + |
| 176 | + active = efi_loadopt_attrs(load_option) & LOAD_OPTION_ACTIVE ? 1 : 0; |
| 177 | + json_object_set_new(boot_json, "active", json_boolean(active)); |
| 178 | + json_object_set_new(boot_json, "description", |
| 179 | + json_string((char *)description)); |
| 180 | + json_fill_path(boot_json, load_option, boot->data_size); |
| 181 | + json_array_append_new(vars_json, boot_json); |
| 182 | + } |
| 183 | + |
| 184 | + json_object_set_new(root, "vars", vars_json); |
| 185 | +} |
| 186 | + |
| 187 | +void |
| 188 | +__print_json(list_t *entry_list, ebm_mode mode, char **prefices, char **order_name) |
| 189 | +{ |
| 190 | + json_t *root = json_object(); |
| 191 | + char *json_str = NULL; |
| 192 | + |
| 193 | + switch (mode) { |
| 194 | + case boot: |
| 195 | + json_fill_bootnext(root); |
| 196 | + json_fill_bootcurrent(root); |
| 197 | + json_fill_timeout(root); |
| 198 | + json_fill_order(root, order_name[mode]); |
| 199 | + json_fill_vars(root, prefices[mode], entry_list); |
| 200 | + break; |
| 201 | + case driver: |
| 202 | + case sysprep: |
| 203 | + json_fill_order(root, order_name[mode]); |
| 204 | + json_fill_vars(root, prefices[mode], entry_list); |
| 205 | + break; |
| 206 | + } |
| 207 | + json_str = json_dumps(root, JSON_COMPACT); |
| 208 | + printf("%s\n", json_str); |
| 209 | + free(json_str); |
| 210 | + json_decref(root); |
| 211 | +} |
0 commit comments