0003-main.c-rework-logic-to-find-def1-def2-and-def3-files.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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
  5. The current logic to find def1, def2 and def3 first tries to find them
  6. in the local directory, and if they are not available, find them in
  7. /usr/share.
  8. However, this doesn't work if rpiboot and its related files are
  9. installed, but not in /usr. In order to address this use-case, this
  10. commit reworks the logic to find the file path.
  11. A new function, getfilepath() is created. If the requested file is
  12. available in the current directory, it is used. If not, then the path to
  13. the file is inferred from the location of the currently running
  14. program. I.e if we run /home/foo/sys/bin/rpiboot, then we will search
  15. def1 in usbbootcode.bin in
  16. /home/foo/sys/bin/../share/rpiboot/usbbootcode.bin.
  17. This continues to address the case of an installation in /usr, while
  18. allowing installation in other locations as well.
  19. Submitted-upstream: https://github.com/raspberrypi/usbboot/pull/2
  20. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  21. ---
  22. main.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++-------------
  23. 1 file changed, 48 insertions(+), 13 deletions(-)
  24. diff --git a/main.c b/main.c
  25. index 1b4e042..7c571d6 100755
  26. --- a/main.c
  27. +++ b/main.c
  28. @@ -1,10 +1,12 @@
  29. -#include "libusb-1.0/libusb.h"
  30. +#define _GNU_SOURCE
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. -
  35. +#include <libgen.h>
  36. #include <unistd.h>
  37. +#include "libusb-1.0/libusb.h"
  38. +
  39. int verbose = 0;
  40. int out_ep = 1;
  41. int in_ep = 2;
  42. @@ -146,6 +148,37 @@ int ep_read(unsigned char *buf, int len, libusb_device_handle * usb_device)
  43. return len;
  44. }
  45. +char *getfilepath(char *filename)
  46. +{
  47. + char *progpath, *filepath, *progdir;
  48. + ssize_t len;
  49. +
  50. + /* If file is available locally, use it */
  51. + if (access(filename, F_OK) != -1)
  52. + return filename;
  53. +
  54. + /* Otherwise, use the installed version */
  55. + progpath = malloc(PATH_MAX);
  56. + len = readlink("/proc/self/exe", progpath, PATH_MAX - 1);
  57. + if (len == -1)
  58. + {
  59. + free(progpath);
  60. + return NULL;
  61. + }
  62. +
  63. + progpath[len] = '\0';
  64. + progdir = dirname(progpath);
  65. + if (asprintf(&filepath, "%s/../share/rpiboot/%s", progdir, filename) < 0)
  66. + {
  67. + free(progpath);
  68. + return NULL;
  69. + }
  70. +
  71. + free(progpath);
  72. +
  73. + return filepath;
  74. +}
  75. +
  76. int main(int argc, char *argv[])
  77. {
  78. int result;
  79. @@ -157,13 +190,9 @@ int main(int argc, char *argv[])
  80. int last_serial = -1;
  81. FILE *fp1, *fp2, *fp;
  82. - char def1_inst[] = "/usr/share/rpiboot/usbbootcode.bin";
  83. - char def2_inst[] = "/usr/share/rpiboot/msd.elf";
  84. - char def3_inst[] = "/usr/share/rpiboot/buildroot.elf";
  85. -
  86. - char def1_loc[] = "./usbbootcode.bin";
  87. - char def2_loc[] = "./msd.elf";
  88. - char def3_loc[] = "./buildroot.elf";
  89. + char def1_name[] = "usbbootcode.bin";
  90. + char def2_name[] = "msd.elf";
  91. + char def3_name[] = "buildroot.elf";
  92. char *def1, *def2, *def3;
  93. @@ -171,10 +200,16 @@ int main(int argc, char *argv[])
  94. char *fatimage = NULL, *executable = NULL;
  95. int loop = 0;
  96. -// if local file version exists use it else use installed
  97. - if( access( def1_loc, F_OK ) != -1 ) { def1 = def1_loc; } else { def1 = def1_inst; }
  98. - if( access( def2_loc, F_OK ) != -1 ) { def2 = def2_loc; } else { def2 = def2_inst; }
  99. - if( access( def3_loc, F_OK ) != -1 ) { def3 = def3_loc; } else { def3 = def3_inst; }
  100. + def1 = getfilepath(def1_name);
  101. + def2 = getfilepath(def2_name);
  102. + def3 = getfilepath(def3_name);
  103. +
  104. + if (!def1 || !def2 || !def3)
  105. + {
  106. + fprintf(stderr, "One of %s, %s or %s cannot be found\n",
  107. + def1_name, def2_name, def3_name);
  108. + exit(1);
  109. + }
  110. stage1 = def1;
  111. stage2 = def2;
  112. --
  113. 2.7.4