diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-09-06 12:25:01 +0200 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-09-11 14:09:51 +0200 |
commit | e3a73a504f6a63bf1a716131cf9dea75dc7b0b41 (patch) | |
tree | 7f5c3d265e654221d4f655d90f2e2a65d0250774 /src/json.c | |
parent | 1fa3813b92139458854f33436c3e4af2f8ecae6b (diff) |
rest: add GET /efi
Add GET /efi request to obtain information about the client's
boot entries.
Field inside the /refresh payload
'efi': {
'entries': [
{
"order": 0,
"name": "Boot0000",
"active": false,
"description": "grub"
},
{
"order": 1,
"name": "Boot0001",
"active": true,
"description": "UEFI: PXE IP4 Realtek PCIe GBE Family Controller"
}
]
}
If the client is not a EFI system it won't add the 'efi' field.
If an entry is not in the boot order it won't have the 'order' field.
GET /efi resquest payload structure:
{
'clients': ['10.141.10.21', '10.141.10.22']
}
GET /efi response's structure:
{
'clients': [
{
'ip': '10.141.10.21',
'entries': [
{
"order": 0,
"name": "Boot0000",
"active": false,
"description": "grub"
},
{
"order": 1,
"name": "Boot0001",
"active": true,
"description": "UEFI: PXE IP4 Realtek PCIe GBE Family Controller"
}
]
},
{
'ip': '10.141.10.22',
'entries': []
}
]
}
The client with ip 10.141.10.22 is a BIOS system.
If an entry does not appear in the boot order it won't have the
'order' field.
Diffstat (limited to 'src/json.c')
-rw-r--r-- | src/json.c | 97 |
1 files changed, 97 insertions, 0 deletions
@@ -303,3 +303,100 @@ int og_json_parse_cache(json_t *element, struct list_head *cache_list) return err; } + +void og_boot_entry_free(struct list_head *boot_entry_list) +{ + struct og_boot_entry *boot_entry, *tmp; + + list_for_each_entry_safe(boot_entry, tmp, boot_entry_list, list) { + list_del(&boot_entry->list); + free(boot_entry); + } +} + +int og_json_parse_boot_entries(json_t *json_entries, struct list_head *boot_entry_list) +{ + uint64_t required_flags = OG_PARAM_EFI_NAME | OG_PARAM_EFI_DESCRIPTION | OG_PARAM_EFI_ACTIVE; + struct og_boot_entry *boot_entry; + json_t *json_elem, *value; + uint64_t flags = 0UL; + unsigned long i; + const char *key; + int err = 0; + + if (json_typeof(json_entries) != JSON_ARRAY) + return -1; + + for (i = 0; i < json_array_size(json_entries); i++) { + boot_entry = calloc(1, sizeof(struct og_boot_entry)); + boot_entry->order = UINT64_MAX; + flags = 0UL; + + if (!boot_entry) + return -1; + + json_elem = json_array_get(json_entries, i); + + if (json_typeof(json_elem) != JSON_OBJECT) { + free(boot_entry); + return -1; + } + + json_object_foreach(json_elem, key, value) { + if (!strcmp(key, "name")) { + err = og_json_parse_string(value, &boot_entry->name); + flags |= OG_PARAM_EFI_NAME; + } else if (!strcmp(key, "active")) { + err = og_json_parse_bool(value, &boot_entry->active); + flags |= OG_PARAM_EFI_ACTIVE; + } else if (!strcmp(key, "description")) { + err = og_json_parse_string(value, &boot_entry->description); + flags |= OG_PARAM_EFI_DESCRIPTION; + } else if (!strcmp(key, "order")) { + err = og_json_parse_uint64(value, &boot_entry->order); + } else + err = -1; + + if (err < 0) { + free(boot_entry); + return err; + } + } + + if ((flags & required_flags) != required_flags) { + free(boot_entry); + return -1; + } + + list_add_tail(&boot_entry->list, boot_entry_list); + } + return err; +} + +int og_json_parse_efi(json_t *element, struct list_head *boot_entry_list) +{ + uint64_t required_flags = OG_PARAM_EFI_ENTRIES; + uint64_t flags = 0UL; + const char *key; + json_t *value; + int err = 0; + + if (json_typeof(element) != JSON_OBJECT) + return -1; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "entries")) { + err = og_json_parse_boot_entries(value, boot_entry_list); + flags |= OG_PARAM_EFI_ENTRIES; + } else + err = -1; + + if (err < 0) + return err; + } + + if ((flags & required_flags) != required_flags) + return -1; + + return err; +} |