2 * Copyright (c) 2017 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
31 #include <efivar-dp.h>
42 #include <efivar-dp.h>
45 * Dump the data as ASCII data, which is a pretty
49 asciidump(uint8_t *data, size_t datalen)
55 for (i = 0; i < datalen; i++) {
56 if (isprint(data[i])) {
62 printf("%c", data[i]);
69 printf("%%%02x", data[i]);
76 utf8dump(uint8_t *data, size_t datalen)
82 * NUL terminate the string. Not all strings need it, but some
83 * do and an extra NUL won't change what's printed.
85 ucs2 = malloc(datalen + sizeof(efi_char));
86 memcpy(ucs2, data, datalen);
87 ucs2[datalen / sizeof(efi_char)] = 0;
88 ucs2_to_utf8(ucs2, &utf8);
95 hexdump(uint8_t *data, size_t datalen)
99 for (i = 0; i < datalen; i++) {
103 printf("%04x: ", (int)i);
105 printf("%02x ", data[i]);
111 bindump(uint8_t *data, size_t datalen)
113 write(1, data, datalen);
116 #define LOAD_OPTION_ACTIVE 1
118 #define SIZE(dp, edp) (size_t)((intptr_t)(void *)edp - (intptr_t)(void *)dp)
121 efi_print_load_option(uint8_t *data, size_t datalen, int Aflag, int bflag, int uflag)
123 uint8_t *ep = data + datalen;
124 uint8_t *walker = data;
135 if (datalen < sizeof(attr) + sizeof(fplen) + sizeof(efi_char))
137 // First 4 bytes are attribute flags
138 attr = le32dec(walker);
139 walker += sizeof(attr);
140 // Next two bytes are length of the file paths
141 fplen = le16dec(walker);
142 walker += sizeof(fplen);
143 // Next we have a 0 terminated UCS2 string that we know to be aligned
144 descr = (efi_char *)(intptr_t)(void *)walker;
145 len = ucs2len(descr); // XXX need to sanity check that len < (datalen - (ep - walker) / 2)
146 walker += (len + 1) * sizeof(efi_char);
149 // Now we have fplen bytes worth of file path stuff
155 // Everything left is the binary option args
157 optlen = ep - walker;
158 // We got to here, everything is good
159 printf("%c ", attr & LOAD_OPTION_ACTIVE ? '*' : ' ');
160 ucs2_to_utf8(descr, &str);
163 while (dp < edp && SIZE(dp, edp) > sizeof(efidp_header)) {
164 efidp_format_device_path(buf, sizeof(buf), dp, SIZE(dp, edp));
165 dp = (efidp)((char *)dp + efidp_size(dp));
166 printf(" %s\n", buf);
172 asciidump(opt, optlen);
174 bindump(opt, optlen);
176 utf8dump(opt, optlen);
178 hexdump(opt, optlen);