Commit 45c7237d9dc22704e9e950966b7d1a73ec185006
Committed by
GitHub
1 parent
86f6453d
Fixup for recent firmware inclusion changes (#34)
* Fix check against NULL for a static var actually looking for an empty c string. * Update .gitignore for code change moving msd firmware to static vars. * Fix support for OS X and *BSD after adding static vars for msd firmware.
Showing
4 changed files
with
116 additions
and
2 deletions
.gitignore
fmemopen.c
0 → 100644
| 1 | +// | ||
| 2 | +// Copyright 2011-2014 NimbusKit | ||
| 3 | +// Originally ported from https://github.com/ingenuitas/python-tesseract/blob/master/fmemopen.c | ||
| 4 | +// | ||
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 6 | +// you may not use this file except in compliance with the License. | ||
| 7 | +// You may obtain a copy of the License at | ||
| 8 | +// | ||
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | +// | ||
| 11 | +// Unless required by applicable law or agreed to in writing, software | ||
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | +// See the License for the specific language governing permissions and | ||
| 15 | +// limitations under the License. | ||
| 16 | +// | ||
| 17 | + | ||
| 18 | +#include <stdio.h> | ||
| 19 | +#include <stdlib.h> | ||
| 20 | +#include <string.h> | ||
| 21 | +#include <sys/mman.h> | ||
| 22 | + | ||
| 23 | +struct fmem { | ||
| 24 | + size_t pos; | ||
| 25 | + size_t size; | ||
| 26 | + char *buffer; | ||
| 27 | +}; | ||
| 28 | +typedef struct fmem fmem_t; | ||
| 29 | + | ||
| 30 | +static int readfn(void *handler, char *buf, int size) { | ||
| 31 | + fmem_t *mem = handler; | ||
| 32 | + size_t available = mem->size - mem->pos; | ||
| 33 | + | ||
| 34 | + if (size > (int)available) { | ||
| 35 | + size = available; | ||
| 36 | + } | ||
| 37 | + memcpy(buf, mem->buffer + mem->pos, sizeof(char) * size); | ||
| 38 | + mem->pos += size; | ||
| 39 | + | ||
| 40 | + return size; | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +static int writefn(void *handler, const char *buf, int size) { | ||
| 44 | + fmem_t *mem = handler; | ||
| 45 | + size_t available = mem->size - mem->pos; | ||
| 46 | + | ||
| 47 | + if (size > (int)available) { | ||
| 48 | + size = available; | ||
| 49 | + } | ||
| 50 | + memcpy(mem->buffer + mem->pos, buf, sizeof(char) * size); | ||
| 51 | + mem->pos += size; | ||
| 52 | + | ||
| 53 | + return size; | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +static fpos_t seekfn(void *handler, fpos_t offset, int whence) { | ||
| 57 | + size_t pos; | ||
| 58 | + fmem_t *mem = handler; | ||
| 59 | + | ||
| 60 | + switch (whence) { | ||
| 61 | + case SEEK_SET: { | ||
| 62 | + if (offset >= 0) { | ||
| 63 | + pos = (size_t)offset; | ||
| 64 | + } else { | ||
| 65 | + pos = 0; | ||
| 66 | + } | ||
| 67 | + break; | ||
| 68 | + } | ||
| 69 | + case SEEK_CUR: { | ||
| 70 | + if (offset >= 0 || (size_t)(-offset) <= mem->pos) { | ||
| 71 | + pos = mem->pos + (size_t)offset; | ||
| 72 | + } else { | ||
| 73 | + pos = 0; | ||
| 74 | + } | ||
| 75 | + break; | ||
| 76 | + } | ||
| 77 | + case SEEK_END: pos = mem->size + (size_t)offset; break; | ||
| 78 | + default: return -1; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + if (pos > mem->size) { | ||
| 82 | + return -1; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + mem->pos = pos; | ||
| 86 | + return (fpos_t)pos; | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +static int closefn(void *handler) { | ||
| 90 | + free(handler); | ||
| 91 | + return 0; | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +FILE *fmemopen(void *buf, size_t size, const char *mode) { | ||
| 95 | + #pragma unused(mode) | ||
| 96 | + // This data is released on fclose. | ||
| 97 | + fmem_t* mem = (fmem_t *) malloc(sizeof(fmem_t)); | ||
| 98 | + | ||
| 99 | + // Zero-out the structure. | ||
| 100 | + memset(mem, 0, sizeof(fmem_t)); | ||
| 101 | + | ||
| 102 | + mem->size = size; | ||
| 103 | + mem->buffer = buf; | ||
| 104 | + | ||
| 105 | + // funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html | ||
| 106 | + return funopen(mem, readfn, writefn, seekfn, closefn); | ||
| 107 | +} |
main.c
| @@ -8,13 +8,18 @@ | @@ -8,13 +8,18 @@ | ||
| 8 | #include "msd/bootcode.h" | 8 | #include "msd/bootcode.h" |
| 9 | #include "msd/start.h" | 9 | #include "msd/start.h" |
| 10 | 10 | ||
| 11 | +/* Assume BSD without native fmemopen() if doesn't seem to be glibc */ | ||
| 12 | +#if defined(__APPLE__) || !defined(_GNU_SOURCE) || !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L | ||
| 13 | +#include "fmemopen.c" // BSD fmemopen() compat in terms of funopen() | ||
| 14 | +#endif | ||
| 15 | + | ||
| 11 | int signed_boot = 0; | 16 | int signed_boot = 0; |
| 12 | int verbose = 0; | 17 | int verbose = 0; |
| 13 | int loop = 0; | 18 | int loop = 0; |
| 14 | int overlay = 0; | 19 | int overlay = 0; |
| 15 | long delay = 500; | 20 | long delay = 500; |
| 16 | char * directory = NULL; | 21 | char * directory = NULL; |
| 17 | -char pathname[18]; | 22 | +char pathname[18] = {0}; |
| 18 | uint8_t targetPortNo = 99; | 23 | uint8_t targetPortNo = 99; |
| 19 | 24 | ||
| 20 | int out_ep; | 25 | int out_ep; |
| @@ -413,7 +418,7 @@ FILE * check_file(char * dir, char *fname) | @@ -413,7 +418,7 @@ FILE * check_file(char * dir, char *fname) | ||
| 413 | // Check directory first then /usr/share/rpiboot | 418 | // Check directory first then /usr/share/rpiboot |
| 414 | if(dir) | 419 | if(dir) |
| 415 | { | 420 | { |
| 416 | - if(overlay&&(pathname != NULL)) | 421 | + if(overlay&&(pathname[0] != 0)) |
| 417 | { | 422 | { |
| 418 | strcpy(path, dir); | 423 | strcpy(path, dir); |
| 419 | strcat(path, "/"); | 424 | strcat(path, "/"); |
msd/.gitignore
0 → 100644
| 1 | +*.h |