1 From 935894908dc24acda0acea7d211a9d80e55ecadb Mon Sep 17 00:00:00 2001
2 From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
3 Date: Fri, 2 Dec 2016 23:43:23 +0100
4 Subject: [PATCH] main.c: rework logic to find def1, def2 and def3 files
6 The current logic to find def1, def2 and def3 first tries to find them
7 in the local directory, and if they are not available, find them in
10 However, this doesn't work if rpiboot and its related files are
11 installed, but not in /usr. In order to address this use-case, this
12 commit reworks the logic to find the file path.
14 A new function, getfilepath() is created. If the requested file is
15 available in the current directory, it is used. If not, then the path to
16 the file is inferred from the location of the currently running
17 program. I.e if we run /home/foo/sys/bin/rpiboot, then we will search
18 def1 in usbbootcode.bin in
19 /home/foo/sys/bin/../share/rpiboot/usbbootcode.bin.
21 This continues to address the case of an installation in /usr, while
22 allowing installation in other locations as well.
24 Submitted-upstream: https://github.com/raspberrypi/usbboot/pull/2
25 Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
27 main.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++-------------
28 1 file changed, 48 insertions(+), 13 deletions(-)
30 diff --git a/main.c b/main.c
31 index 1b4e042..7c571d6 100755
35 -#include "libusb-1.0/libusb.h"
44 +#include "libusb-1.0/libusb.h"
49 @@ -146,6 +148,37 @@ int ep_read(unsigned char *buf, int len, libusb_device_handle * usb_device)
53 +char *getfilepath(char *filename)
55 + char *progpath, *filepath, *progdir;
58 + /* If file is available locally, use it */
59 + if (access(filename, F_OK) != -1)
62 + /* Otherwise, use the installed version */
63 + progpath = malloc(PATH_MAX);
64 + len = readlink("/proc/self/exe", progpath, PATH_MAX - 1);
71 + progpath[len] = '\0';
72 + progdir = dirname(progpath);
73 + if (asprintf(&filepath, "%s/../share/rpiboot/%s", progdir, filename) < 0)
84 int main(int argc, char *argv[])
87 @@ -157,13 +190,9 @@ int main(int argc, char *argv[])
91 - char def1_inst[] = "/usr/share/rpiboot/usbbootcode.bin";
92 - char def2_inst[] = "/usr/share/rpiboot/msd.elf";
93 - char def3_inst[] = "/usr/share/rpiboot/buildroot.elf";
95 - char def1_loc[] = "./usbbootcode.bin";
96 - char def2_loc[] = "./msd.elf";
97 - char def3_loc[] = "./buildroot.elf";
98 + char def1_name[] = "usbbootcode.bin";
99 + char def2_name[] = "msd.elf";
100 + char def3_name[] = "buildroot.elf";
102 char *def1, *def2, *def3;
104 @@ -171,10 +200,16 @@ int main(int argc, char *argv[])
105 char *fatimage = NULL, *executable = NULL;
108 -// if local file version exists use it else use installed
109 - if( access( def1_loc, F_OK ) != -1 ) { def1 = def1_loc; } else { def1 = def1_inst; }
110 - if( access( def2_loc, F_OK ) != -1 ) { def2 = def2_loc; } else { def2 = def2_inst; }
111 - if( access( def3_loc, F_OK ) != -1 ) { def3 = def3_loc; } else { def3 = def3_inst; }
112 + def1 = getfilepath(def1_name);
113 + def2 = getfilepath(def2_name);
114 + def3 = getfilepath(def3_name);
116 + if (!def1 || !def2 || !def3)
118 + fprintf(stderr, "One of %s, %s or %s cannot be found\n",
119 + def1_name, def2_name, def3_name);