Skip to content

Commit d052e1c

Browse files
mhiramatrostedt
authored andcommitted
tools/bootconfig: Show bootconfig compact tree from bootconfig file
Show the bootconfig compact tree from the bootconfig file instead of an initrd if the given file has no magic number and is smaller than 32KB. User can use this for checking the syntax error or output checking before applying the bootconfig to initrd. Link: https://lkml.kernel.org/r/159704848156.175360.6621139371000789360.stgit@devnote2 Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent a27026e commit d052e1c

1 file changed

Lines changed: 58 additions & 23 deletions

File tree

‎tools/bootconfig/main.c‎

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,55 @@ int load_xbc_from_initrd(int fd, char **buf)
195195
return size;
196196
}
197197

198+
static void show_xbc_error(const char *data, const char *msg, int pos)
199+
{
200+
int lin = 1, col, i;
201+
202+
if (pos < 0) {
203+
pr_err("Error: %s.\n", msg);
204+
return;
205+
}
206+
207+
/* Note that pos starts from 0 but lin and col should start from 1. */
208+
col = pos + 1;
209+
for (i = 0; i < pos; i++) {
210+
if (data[i] == '\n') {
211+
lin++;
212+
col = pos - i;
213+
}
214+
}
215+
pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
216+
217+
}
218+
219+
static int init_xbc_with_error(char *buf, int len)
220+
{
221+
char *copy = strdup(buf);
222+
const char *msg;
223+
int ret, pos;
224+
225+
if (!copy)
226+
return -ENOMEM;
227+
228+
ret = xbc_init(buf, &msg, &pos);
229+
if (ret < 0)
230+
show_xbc_error(copy, msg, pos);
231+
free(copy);
232+
233+
return ret;
234+
}
235+
198236
int show_xbc(const char *path)
199237
{
200238
int ret, fd;
201239
char *buf = NULL;
240+
struct stat st;
241+
242+
ret = stat(path, &st);
243+
if (ret < 0) {
244+
pr_err("Failed to stat %s: %d\n", path, -errno);
245+
return -errno;
246+
}
202247

203248
fd = open(path, O_RDONLY);
204249
if (fd < 0) {
@@ -207,14 +252,24 @@ int show_xbc(const char *path)
207252
}
208253

209254
ret = load_xbc_from_initrd(fd, &buf);
255+
close(fd);
210256
if (ret < 0) {
211257
pr_err("Failed to load a boot config from initrd: %d\n", ret);
212258
goto out;
213259
}
260+
/* Assume a bootconfig file if it is enough small */
261+
if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
262+
ret = load_xbc_file(path, &buf);
263+
if (ret < 0) {
264+
pr_err("Failed to load a boot config: %d\n", ret);
265+
goto out;
266+
}
267+
if (init_xbc_with_error(buf, ret) < 0)
268+
goto out;
269+
}
214270
xbc_show_compact_tree();
215271
ret = 0;
216272
out:
217-
close(fd);
218273
free(buf);
219274

220275
return ret;
@@ -251,27 +306,6 @@ int delete_xbc(const char *path)
251306
return ret;
252307
}
253308

254-
static void show_xbc_error(const char *data, const char *msg, int pos)
255-
{
256-
int lin = 1, col, i;
257-
258-
if (pos < 0) {
259-
pr_err("Error: %s.\n", msg);
260-
return;
261-
}
262-
263-
/* Note that pos starts from 0 but lin and col should start from 1. */
264-
col = pos + 1;
265-
for (i = 0; i < pos; i++) {
266-
if (data[i] == '\n') {
267-
lin++;
268-
col = pos - i;
269-
}
270-
}
271-
pr_err("Parse Error: %s at %d:%d\n", msg, lin, col);
272-
273-
}
274-
275309
int apply_xbc(const char *path, const char *xbc_path)
276310
{
277311
u32 size, csum;
@@ -352,11 +386,12 @@ int apply_xbc(const char *path, const char *xbc_path)
352386
int usage(void)
353387
{
354388
printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
389+
"Or bootconfig <CONFIG>\n"
355390
" Apply, delete or show boot config to initrd.\n"
356391
" Options:\n"
357392
" -a <config>: Apply boot config to initrd\n"
358393
" -d : Delete boot config file from initrd\n\n"
359-
" If no option is given, show current applied boot config.\n");
394+
" If no option is given, show the bootconfig in the given file.\n");
360395
return -1;
361396
}
362397

0 commit comments

Comments
 (0)