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