Skip to content

Commit 5854ed9

Browse files
committed
lk2nd: boot: Allow relative paths in extlinux commands
1 parent b9f62db commit 5854ed9

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

lk2nd/boot/extlinux.c

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,28 @@ static bool fs_file_exists(const char *file)
335335
return true;
336336
}
337337

338+
/**
339+
* normalize_path() - Normalize path against given root.
340+
*
341+
* Given some path (absolute or relative), normalize it to
342+
* the lk "vfs" path; prepending /extlinux/ if the path is
343+
* relative.
344+
*
345+
* Returns: Newly allocated copy of the string with normalized
346+
* path.
347+
*/
348+
static char *normalize_path(const char *path, const char *root)
349+
{
350+
char tmp[256];
351+
352+
if (path[0] == '/')
353+
snprintf(tmp, sizeof(tmp), "%s/%s", root, path);
354+
else
355+
snprintf(tmp, sizeof(tmp), "%s/extlinux/%s", root, path);
356+
357+
return strndup(tmp, sizeof(tmp));
358+
}
359+
338360
/**
339361
* expand_conf() - Sanity check and rewrite the parsed config.
340362
*
@@ -348,7 +370,6 @@ static bool fs_file_exists(const char *file)
348370
static bool expand_conf(struct label *label, const char *root)
349371
{
350372
const char *const *dtbfiles = lk2nd_device_get_dtb_hints();
351-
char path[128];
352373
int i = 0;
353374

354375
/* Cant boot without any kernel. */
@@ -357,8 +378,7 @@ static bool expand_conf(struct label *label, const char *root)
357378
return false;
358379
}
359380

360-
snprintf(path, sizeof(path), "%s/%s", root, label->kernel);
361-
label->kernel = strndup(path, sizeof(path));
381+
label->kernel = normalize_path(label->kernel, root);
362382

363383
if (!fs_file_exists(label->kernel)) {
364384
dprintf(INFO, "Kernel %s does not exist\n", label->kernel);
@@ -378,24 +398,40 @@ static bool expand_conf(struct label *label, const char *root)
378398
}
379399

380400
while (dtbfiles[i]) {
381-
/* NOTE: Try aarch64 path, then aarch32 one. */
382-
snprintf(path, sizeof(path), "%s/%s/qcom/%s.dtb", root, label->dtbdir, dtbfiles[i]);
383-
if (!fs_file_exists(path))
384-
snprintf(path, sizeof(path), "%s/%s/qcom-%s.dtb", root, label->dtbdir, dtbfiles[i]);
401+
char dtb[128];
402+
char *normalized = NULL;
403+
404+
/* Try arm64 style path. */
405+
snprintf(dtb, sizeof(dtb), "%s/qcom/%s.dtb", label->dtbdir, dtbfiles[i]);
406+
normalized = normalize_path(dtb, root);
407+
if (fs_file_exists(normalized)) {
408+
label->dtb = normalized;
409+
break;
410+
}
411+
free(normalized);
385412

386-
/* boot-deploy drops the vendor dir when copying dtbs. */
387-
if (!fs_file_exists(path))
388-
snprintf(path, sizeof(path), "%s/%s/%s.dtb", root, label->dtbdir, dtbfiles[i]);
413+
/* Try arm32 style path. */
414+
snprintf(dtb, sizeof(dtb), "%s/qcom-%s.dtb", label->dtbdir, dtbfiles[i]);
415+
normalized = normalize_path(dtb, root);
416+
if (fs_file_exists(normalized)) {
417+
label->dtb = normalized;
418+
break;
419+
}
420+
free(normalized);
389421

390-
if (fs_file_exists(path)) {
391-
label->dtb = strndup(path, sizeof(path));
422+
/* boot-deploy drops the vendor dir when copying dtbs. */
423+
snprintf(dtb, sizeof(dtb), "%s/%s.dtb", label->dtbdir, dtbfiles[i]);
424+
normalized = normalize_path(dtb, root);
425+
if (fs_file_exists(normalized)) {
426+
label->dtb = normalized;
392427
break;
393428
}
429+
free(normalized);
430+
394431
i++;
395432
}
396433
} else {
397-
snprintf(path, sizeof(path), "%s/%s", root, label->dtb);
398-
label->dtb = strndup(path, sizeof(path));
434+
label->dtb = normalize_path(label->dtb, root);
399435
}
400436

401437
if (!fs_file_exists(label->dtb)) {
@@ -406,20 +442,18 @@ static bool expand_conf(struct label *label, const char *root)
406442
if (label->dtboverlays) {
407443
i = 0;
408444
while (label->dtboverlays[i]) {
409-
snprintf(path, sizeof(path), "%s/%s", root, label->dtboverlays[i]);
410-
if (fs_file_exists(path)) {
411-
label->dtboverlays[i] = strndup(path, sizeof(path));
412-
} else {
445+
label->dtboverlays[i] = normalize_path(label->dtboverlays[i], root);
446+
if (!fs_file_exists(label->dtboverlays[i])) {
413447
dprintf(INFO, "FDT overlay %s does not exist\n", label->dtboverlays[i]);
414448
return false;
415449
}
450+
416451
i++;
417452
}
418453
}
419454

420455
if (label->initramfs) {
421-
snprintf(path, sizeof(path), "%s/%s", root, label->initramfs);
422-
label->initramfs = strndup(path, sizeof(path));
456+
label->initramfs = normalize_path(label->initramfs, root);
423457

424458
if (!fs_file_exists(label->initramfs)) {
425459
dprintf(INFO, "Initramfs %s does not exist\n", label->initramfs);

0 commit comments

Comments
 (0)