Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup for recent firmware inclusion changes #34

Merged
merged 3 commits into from Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
rpiboot
bin2c
107 changes: 107 additions & 0 deletions fmemopen.c
@@ -0,0 +1,107 @@
//
// Copyright 2011-2014 NimbusKit
// Originally ported from https://github.com/ingenuitas/python-tesseract/blob/master/fmemopen.c
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

struct fmem {
size_t pos;
size_t size;
char *buffer;
};
typedef struct fmem fmem_t;

static int readfn(void *handler, char *buf, int size) {
fmem_t *mem = handler;
size_t available = mem->size - mem->pos;

if (size > (int)available) {
size = available;
}
memcpy(buf, mem->buffer + mem->pos, sizeof(char) * size);
mem->pos += size;

return size;
}

static int writefn(void *handler, const char *buf, int size) {
fmem_t *mem = handler;
size_t available = mem->size - mem->pos;

if (size > (int)available) {
size = available;
}
memcpy(mem->buffer + mem->pos, buf, sizeof(char) * size);
mem->pos += size;

return size;
}

static fpos_t seekfn(void *handler, fpos_t offset, int whence) {
size_t pos;
fmem_t *mem = handler;

switch (whence) {
case SEEK_SET: {
if (offset >= 0) {
pos = (size_t)offset;
} else {
pos = 0;
}
break;
}
case SEEK_CUR: {
if (offset >= 0 || (size_t)(-offset) <= mem->pos) {
pos = mem->pos + (size_t)offset;
} else {
pos = 0;
}
break;
}
case SEEK_END: pos = mem->size + (size_t)offset; break;
default: return -1;
}

if (pos > mem->size) {
return -1;
}

mem->pos = pos;
return (fpos_t)pos;
}

static int closefn(void *handler) {
free(handler);
return 0;
}

FILE *fmemopen(void *buf, size_t size, const char *mode) {
#pragma unused(mode)
// This data is released on fclose.
fmem_t* mem = (fmem_t *) malloc(sizeof(fmem_t));

// Zero-out the structure.
memset(mem, 0, sizeof(fmem_t));

mem->size = size;
mem->buffer = buf;

// funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
return funopen(mem, readfn, writefn, seekfn, closefn);
}
9 changes: 7 additions & 2 deletions main.c
Expand Up @@ -8,13 +8,18 @@
#include "msd/bootcode.h"
#include "msd/start.h"

/* Assume BSD without native fmemopen() if doesn't seem to be glibc */
#if defined(__APPLE__) || !defined(_GNU_SOURCE) || !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L
#include "fmemopen.c" // BSD fmemopen() compat in terms of funopen()
#endif

int signed_boot = 0;
int verbose = 0;
int loop = 0;
int overlay = 0;
long delay = 500;
char * directory = NULL;
char pathname[18];
char pathname[18] = {0};
uint8_t targetPortNo = 99;

int out_ep;
Expand Down Expand Up @@ -391,7 +396,7 @@ FILE * check_file(char * dir, char *fname)
// Check directory first then /usr/share/rpiboot
if(dir)
{
if(overlay&&(pathname != NULL))
if(overlay&&(pathname[0] != 0))
{
strcpy(path, dir);
strcat(path, "/");
Expand Down
1 change: 1 addition & 0 deletions msd/.gitignore
@@ -0,0 +1 @@
*.h