Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions usbboot/Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
rpiboot: main.c
$(CC) -g -o $@ $< -lusb-1.0
$(CC) -g $(CFLAGS) -o $@ $< -lusb-1.0 $(LDFLAGS)

install: rpiboot
cp rpiboot /usr/bin
mkdir -p /usr/share/rpiboot
cp usbbootcode.bin /usr/share/rpiboot
cp msd.elf /usr/share/rpiboot
cp buildroot.elf /usr/share/rpiboot
cp rpiboot $(DESTDIR)/usr/bin
mkdir -p $(DESTDIR)//usr/share/rpiboot
cp usbbootcode.bin $(DESTDIR)/usr/share/rpiboot
cp msd.elf $(DESTDIR)/usr/share/rpiboot
cp buildroot.elf $(DESTDIR)/usr/share/rpiboot

uninstall:
rm -f /usr/bin/rpiboot
rm -f /usr/share/rpiboot/usbbootcode.bin
rm -f /usr/share/rpiboot/msd.elf
rm -f /usr/share/rpiboot/buildroot.elf
rmdir --ignore-fail-on-non-empty /usr/share/rpiboot/
rm -f $(DESTDIR)/usr/bin/rpiboot
rm -f $(DESTDIR)/usr/share/rpiboot/usbbootcode.bin
rm -f $(DESTDIR)/usr/share/rpiboot/msd.elf
rm -f $(DESTDIR)/usr/share/rpiboot/buildroot.elf
rmdir --ignore-fail-on-non-empty $(DESTDIR)/usr/share/rpiboot/

clean:
rm rpiboot
Expand Down
61 changes: 48 additions & 13 deletions usbboot/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "libusb-1.0/libusb.h"
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libgen.h>
#include <unistd.h>

#include "libusb-1.0/libusb.h"

int verbose = 0;
int out_ep = 1;
int in_ep = 2;
Expand Down Expand Up @@ -146,6 +148,37 @@ int ep_read(unsigned char *buf, int len, libusb_device_handle * usb_device)
return len;
}

char *getfilepath(char *filename)
{
char *progpath, *filepath, *progdir;
ssize_t len;

/* If file is available locally, use it */
if (access(filename, F_OK) != -1)
return filename;

/* Otherwise, use the installed version */
progpath = malloc(PATH_MAX);
len = readlink("/proc/self/exe", progpath, PATH_MAX - 1);
if (len == -1)
{
free(progpath);
return NULL;
}

progpath[len] = '\0';
progdir = dirname(progpath);
if (asprintf(&filepath, "%s/../share/rpiboot/%s", progdir, filename) < 0)
{
free(progpath);
return NULL;
}

free(progpath);

return filepath;
}

int main(int argc, char *argv[])
{
int result;
Expand All @@ -157,24 +190,26 @@ int main(int argc, char *argv[])
int last_serial = -1;
FILE *fp1, *fp2, *fp;

char def1_inst[] = "/usr/share/rpiboot/usbbootcode.bin";
char def2_inst[] = "/usr/share/rpiboot/msd.elf";
char def3_inst[] = "/usr/share/rpiboot/buildroot.elf";

char def1_loc[] = "./usbbootcode.bin";
char def2_loc[] = "./msd.elf";
char def3_loc[] = "./buildroot.elf";
char def1_name[] = "usbbootcode.bin";
char def2_name[] = "msd.elf";
char def3_name[] = "buildroot.elf";

char *def1, *def2, *def3;

char *stage1 = NULL, *stage2 = NULL;
char *fatimage = NULL, *executable = NULL;
int loop = 0;

// if local file version exists use it else use installed
if( access( def1_loc, F_OK ) != -1 ) { def1 = def1_loc; } else { def1 = def1_inst; }
if( access( def2_loc, F_OK ) != -1 ) { def2 = def2_loc; } else { def2 = def2_inst; }
if( access( def3_loc, F_OK ) != -1 ) { def3 = def3_loc; } else { def3 = def3_inst; }
def1 = getfilepath(def1_name);
def2 = getfilepath(def2_name);
def3 = getfilepath(def3_name);

if (!def1 || !def2 || !def3)
{
fprintf(stderr, "One of %s, %s or %s cannot be found\n",
def1_name, def2_name, def3_name);
exit(1);
}

stage1 = def1;
stage2 = def2;
Expand Down