Skip to content

Commit 3e12f3f

Browse files
author
Alejandro Sirgo Rica
committed
Add dp field to JSON output
Add "dp" field to every entry with the device path information available in the -v flag as structure json fields. WIP Signed-off-by: Alejandro Sirgo Rica <asirgo@soleta.eu>
1 parent 13b56d0 commit 3e12f3f

11 files changed

Lines changed: 1414 additions & 2 deletions

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ all : deps $(TARGETS)
1212

1313
EFIBOOTMGR_SOURCES = efibootmgr.c efi.c parse_loader_data.c
1414
ifdef JSON
15-
EFIBOOTMGR_SOURCES += json.c
15+
EFIBOOTMGR_SOURCES += json.c jsondp.c jsondp_message.c json_utils.c
1616
endif
1717
EFICONMAN_SOURCES = eficonman.c
1818
EFIBOOTDUMP_SOURCES = efibootdump.c parse_loader_data.c

src/efibootmgr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ set_order(const char *order_name, const char *prefix, int keep_old_entries)
926926
#define ev_bits(val, mask, shift) \
927927
(((val) & ((mask) << (shift))) >> (shift))
928928

929-
static inline char *
929+
char *
930930
ucs2_to_utf8(const uint16_t * const chars, ssize_t limit)
931931
{
932932
ssize_t i, j;

src/efibootmgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,4 @@ extern efibootmgr_opt_t opts;
114114

115115
int read_u16(const char *name);
116116
int read_order(const char *name, var_entry_t **order);
117+
char *ucs2_to_utf8(const uint16_t * const chars, ssize_t limit);

src/efivar_endian.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/*
3+
* Copyright 2009-2015 Red Hat, Inc.
4+
*
5+
* Author: Peter Jones <pjones@redhat.com>
6+
*/
7+
#pragma once
8+
9+
#include <endian.h>
10+
11+
#if __BYTE_ORDER == __LITTLE_ENDIAN
12+
#define cpu_to_le16(x) (x)
13+
#define cpu_to_le32(x) (x)
14+
#define cpu_to_le64(x) ((uint64_t)x)
15+
#define le16_to_cpu(x) (x)
16+
#define le32_to_cpu(x) (x)
17+
#define le64_to_cpu(x) ((uint64_t)x)
18+
#define cpu_to_be16(x) __builtin_bswap16(x)
19+
#define cpu_to_be32(x) __builtin_bswap32(x)
20+
#define cpu_to_be64(x) ((uint64_t)__builtin_bswap64(x))
21+
#define be16_to_cpu(x) __builtin_bswap16(x)
22+
#define be32_to_cpu(x) __builtin_bswap32(x)
23+
#define be64_to_cpu(x) ((uint64_t)__builtin_bswap64(x))
24+
#else
25+
#define cpu_to_be16(x) (x)
26+
#define cpu_to_be32(x) (x)
27+
#define cpu_to_be64(x) ((uint64_t)x)
28+
#define be16_to_cpu(x) (x)
29+
#define be32_to_cpu(x) (x)
30+
#define be64_to_cpu(x) ((uint64_t)x)
31+
#define cpu_to_le16(x) __builtin_bswap16(x)
32+
#define cpu_to_le32(x) __builtin_bswap32(x)
33+
#define cpu_to_le64(x) ((uint64_t)__builtin_bswap64(x))
34+
#define le16_to_cpu(x) __builtin_bswap16(x)
35+
#define le32_to_cpu(x) __builtin_bswap32(x)
36+
#define le64_to_cpu(x) ((uint64_t)__builtin_bswap64(x))
37+
#endif
38+
39+
// vim:fenc=utf-8:tw=75:noet

src/json.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "parse_loader_data.h"
55
#include "efibootmgr.h"
6+
#include "jsondp.h"
67
#include "error.h"
78
#include "json.h"
89

@@ -140,6 +141,7 @@ json_fill_vars(json_t *root, const char *prefix, list_t *entry_list)
140141
json_object_set_new(boot_json, "active", json_boolean(active));
141142
json_object_set_new(boot_json, "description",
142143
json_string((char *)description));
144+
json_fill_device_path(boot_json, load_option, boot->data_size);
143145
json_array_append_new(vars_json, boot_json);
144146
}
145147

src/json_utils.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <efivar.h>
2+
3+
#include "json_utils.h"
4+
5+
static char *
6+
hex_to_string(const void *addr, size_t len,
7+
const char *separator, int stride)
8+
{
9+
size_t sep_len = separator ? strlen(separator) : 0;
10+
const unsigned char *bytes = addr;
11+
size_t out_len = len * 2;
12+
char *str, *p;
13+
14+
if (separator && stride > 0 && len > 1)
15+
out_len += ((len - 1) / stride) * sep_len;
16+
17+
str = malloc(out_len + 1);
18+
if (!str)
19+
return NULL;
20+
21+
p = str;
22+
23+
for (size_t i = 0; i < len; i++) {
24+
if (i && separator && stride > 0 && i % stride == 0) {
25+
memcpy(p, separator, sep_len);
26+
p += sep_len;
27+
}
28+
29+
sprintf(p, "%02x", bytes[i]);
30+
p += 2;
31+
}
32+
33+
*p = '\0';
34+
return str;
35+
}
36+
37+
json_t *
38+
json_hex(const void *addr, size_t len, const char *sep, int stride)
39+
{
40+
char *s = hex_to_string(addr, len, sep, stride);
41+
json_t *j;
42+
43+
if (!s)
44+
return NULL;
45+
46+
j = json_string(s);
47+
free(s);
48+
return j;
49+
}
50+
51+
json_t *
52+
json_ipv4(const uint8_t ip[4])
53+
{
54+
char buf[16];
55+
56+
snprintf(buf, sizeof(buf),
57+
"%u.%u.%u.%u",
58+
ip[0], ip[1], ip[2], ip[3]);
59+
60+
return json_string(buf);
61+
}
62+
63+
json_t *
64+
json_guid(const void *guid_data)
65+
{
66+
efi_guid_t guid;
67+
char *guid_str;
68+
json_t *res;
69+
70+
memcpy(&guid, guid_data, sizeof(guid));
71+
72+
if (efi_guid_to_str(&guid, &guid_str) < 0)
73+
return NULL;
74+
75+
res = json_string(guid_str);
76+
free(guid_str);
77+
return res;
78+
}

src/json_utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <jansson.h>
2+
#include <stdlib.h>
3+
4+
#ifdef JSON
5+
json_t *json_hex(const void *addr, size_t len, const char *sep, int stride);
6+
7+
json_t *json_ipv4(const uint8_t ip[4]);
8+
9+
json_t *json_guid(const void *guid_data);
10+
#endif

0 commit comments

Comments
 (0)