board/csky: fixup gdb instructions in readme.txt
[buildroot-gz.git] / package / raspberrypi-usbboot / 0003-main.c-rework-logic-to-find-def1-def2-and-def3-files.patch
blob30cde49f3471ac5662c139aab1ff5e80e46d447c
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
8 /usr/share.
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>
26 ---
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
32 --- a/main.c
33 +++ b/main.c
34 @@ -1,10 +1,12 @@
35 -#include "libusb-1.0/libusb.h"
36 +#define _GNU_SOURCE
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 +#include <libgen.h>
42 #include <unistd.h>
44 +#include "libusb-1.0/libusb.h"
46 int verbose = 0;
47 int out_ep = 1;
48 int in_ep = 2;
49 @@ -146,6 +148,37 @@ int ep_read(unsigned char *buf, int len, libusb_device_handle * usb_device)
50 return len;
53 +char *getfilepath(char *filename)
55 + char *progpath, *filepath, *progdir;
56 + ssize_t len;
58 + /* If file is available locally, use it */
59 + if (access(filename, F_OK) != -1)
60 + return filename;
62 + /* Otherwise, use the installed version */
63 + progpath = malloc(PATH_MAX);
64 + len = readlink("/proc/self/exe", progpath, PATH_MAX - 1);
65 + if (len == -1)
66 + {
67 + free(progpath);
68 + return NULL;
69 + }
71 + progpath[len] = '\0';
72 + progdir = dirname(progpath);
73 + if (asprintf(&filepath, "%s/../share/rpiboot/%s", progdir, filename) < 0)
74 + {
75 + free(progpath);
76 + return NULL;
77 + }
79 + free(progpath);
81 + return filepath;
84 int main(int argc, char *argv[])
86 int result;
87 @@ -157,13 +190,9 @@ int main(int argc, char *argv[])
88 int last_serial = -1;
89 FILE *fp1, *fp2, *fp;
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;
106 int loop = 0;
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);
120 + exit(1);
123 stage1 = def1;
124 stage2 = def2;
126 2.7.4