diff options
Diffstat (limited to 'lib/x1000-installer/src')
-rw-r--r-- | lib/x1000-installer/src/xf_error.c | 42 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_flashmap.c | 327 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_nandio.c | 292 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_package.c | 261 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_stream.c | 211 | ||||
-rw-r--r-- | lib/x1000-installer/src/xf_update.c | 149 |
6 files changed, 0 insertions, 1282 deletions
diff --git a/lib/x1000-installer/src/xf_error.c b/lib/x1000-installer/src/xf_error.c deleted file mode 100644 index 3d4b342a92..0000000000 --- a/lib/x1000-installer/src/xf_error.c +++ /dev/null @@ -1,42 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2021 Aidan MacDonald - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ - -#include "xf_error.h" - -const char* xf_strerror(int err) -{ - switch(err) { - case XF_E_SUCCESS: return "Success"; - case XF_E_IO: return "I/O error"; - case XF_E_LINE_TOO_LONG: return "Line too long"; - case XF_E_FILENAME_TOO_LONG: return "Filename too long"; - case XF_E_INT_OVERFLOW: return "Numeric overflow"; - case XF_E_BUF_OVERFLOW: return "Buffer overflowed"; - case XF_E_SYNTAX_ERROR: return "Syntax error"; - case XF_E_INVALID_PARAMETER: return "Invalid parameter"; - case XF_E_NAND: return "NAND flash error"; - case XF_E_OUT_OF_MEMORY: return "Out of memory"; - case XF_E_OUT_OF_RANGE: return "Out of range"; - case XF_E_VERIFY_FAILED: return "Verification failed"; - case XF_E_CANNOT_OPEN_FILE: return "Cannot open file"; - default: return "Unknown error"; - } -} diff --git a/lib/x1000-installer/src/xf_flashmap.c b/lib/x1000-installer/src/xf_flashmap.c deleted file mode 100644 index 972bf320ad..0000000000 --- a/lib/x1000-installer/src/xf_flashmap.c +++ /dev/null @@ -1,327 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2021 Aidan MacDonald - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ - -#include "xf_flashmap.h" -#include "xf_error.h" -#include <stdbool.h> -#include <stdlib.h> -#include <ctype.h> -#include <string.h> - -static int xdigit_to_int(char c) -{ - if(c >= 'a' && c <= 'f') - return 10 + (c - 'a'); - if(c >= 'A' && c <= 'F') - return 10 + (c - 'A'); - if(c >= '0' && c <= '9') - return c - '0'; - return -1; -} - -static int isfilenamechar(char c) -{ - return (c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c >= '0' && c <= '9') || - c == '$' || c == '%' || c == '\'' || c == '-' || c == '_' || - c == '@' || c == '~' || c == '`' || c == '!' || c == '(' || - c == ')' || c == '{' || c == '}' || c == '^' || c == '#' || - c == '&' || c == '+' || c == ',' || c == ';' || c == '=' || - c == '[' || c == ']' || c == '.'; -} - -int xf_map_parseline(const char* line, struct xf_map* map) -{ - enum { - s_name, - s_md5, - s_first_num, - s_file_len = s_first_num, - s_offset, - s_length, - s_done, - }; - -#define skipws() do { while(*line == ' ' || *line == '\t') ++line; } while(0) -#define nextstate() do { ++state; length = 0; ++line; skipws(); } while(0) - - int state = s_name; - int length = 0; - int digit_val; - uint32_t int_val; - uint32_t* num_ptr[3] = {&map->file_length, &map->offset, &map->length}; - bool has_md5 = true; - - skipws(); - while(*line && *line != '\n') { - switch(state) { - case s_name: - if(*line == ' ' || *line == '\t') { - nextstate(); - continue; - } else if(isfilenamechar(*line)) { - if(length == XF_MAP_NAMELEN) - return XF_E_FILENAME_TOO_LONG; - - map->name[length++] = *line++; - map->name[length] = '\0'; - continue; - } else { - return XF_E_SYNTAX_ERROR; - } - - case s_md5: - if(*line == '-') { - memset(map->md5, 0, 16); - map->file_length = 0; - has_md5 = false; - ++line; - } else { - for(int i = 0; i < 16; ++i) { - int_val = 0; - for(int j = 0; j < 2; ++j) { - digit_val = xdigit_to_int(*line++); - if(digit_val < 0) - return XF_E_SYNTAX_ERROR; - - int_val <<= 4; - int_val |= digit_val; - } - - map->md5[i] = int_val; - } - } - - if(*line == ' ' || *line == '\t') { - /* skip file length if md5 is not present */ - if(!has_md5) - ++state; - - nextstate(); - continue; - } else { - return XF_E_SYNTAX_ERROR; - } - - case s_file_len: - case s_offset: - case s_length: - int_val = 0; - - if(*line == '0') { - ++line; - if(*line == 'x' || *line == 'X') { - ++line; - while((digit_val = xdigit_to_int(*line)) >= 0) { - ++line; - - if(int_val > UINT32_MAX/16) - return XF_E_INT_OVERFLOW; - - int_val *= 16; - int_val |= digit_val; - } - } - } else if(*line >= '1' && *line <= '9') { - do { - if(int_val > UINT32_MAX/10) - return XF_E_INT_OVERFLOW; - int_val *= 10; - - digit_val = *line++ - '0'; - if(int_val > UINT32_MAX - digit_val) - return XF_E_INT_OVERFLOW; - - int_val += digit_val; - } while(*line >= '0' && *line <= '9'); - } - - *num_ptr[state - s_first_num] = int_val; - - if(*line == ' ' || *line == '\t') { - nextstate(); - continue; - } else if(state+1 == s_done && *line == '\0') { - /* end of input */ - continue; - } else { - return XF_E_SYNTAX_ERROR; - } - - case s_done: - if(isspace(*line)) { - line++; - continue; /* swallow trailing spaces, carriage return, etc */ - } else - return XF_E_SYNTAX_ERROR; - } - } - -#undef skipws -#undef nextstate - - /* one last overflow check - ensure mapped range is addressable */ - if(map->offset > UINT32_MAX - map->length) - return XF_E_INT_OVERFLOW; - - if(has_md5) - map->flags = XF_MAP_HAS_MD5 | XF_MAP_HAS_FILE_LENGTH; - else - map->flags = 0; - - return XF_E_SUCCESS; -} - -struct map_parse_args { - struct xf_map* map; - int num; - int maxnum; -}; - -int map_parse_line_cb(int n, char* buf, void* arg) -{ - (void)n; - - struct map_parse_args* args = arg; - - /* skip whitespace */ - while(*buf && isspace(*buf)) - ++buf; - - /* ignore comments and blank lines */ - if(*buf == '#' || *buf == '\0') - return 0; - - struct xf_map dummy_map; - struct xf_map* dst_map; - if(args->num < args->maxnum) - dst_map = &args->map[args->num]; - else - dst_map = &dummy_map; - - int rc = xf_map_parseline(buf, dst_map); - if(rc) - return rc; - - args->num++; - return 0; -} - -int xf_map_parse(struct xf_stream* s, struct xf_map* map, int maxnum) -{ - char buf[200]; - struct map_parse_args args; - args.map = map; - args.num = 0; - args.maxnum = maxnum; - - int rc = xf_stream_read_lines(s, buf, sizeof(buf), - map_parse_line_cb, &args); - if(rc < 0) - return rc; - - return args.num; -} - -static int xf_map_compare(const void* a, const void* b) -{ - const struct xf_map* mapA = a; - const struct xf_map* mapB = b; - - if(mapA->offset < mapB->offset) - return -1; - else if(mapA->offset == mapB->offset) - return 0; - else - return 1; -} - -int xf_map_sort(struct xf_map* map, int num) -{ - qsort(map, num, sizeof(struct xf_map), xf_map_compare); - return xf_map_validate(map, num); -} - -int xf_map_validate(const struct xf_map* map, int num) -{ - for(int i = 1; i < num; ++i) - if(map[i].offset <= map[i-1].offset) - return -1; - - for(int i = 1; i < num; ++i) - if(map[i-1].offset + map[i-1].length > map[i].offset) - return i; - - return 0; -} - -int xf_map_write(struct xf_map* map, int num, struct xf_stream* s) -{ - static const char hex[] = "0123456789abcdef"; - char buf[200]; - char md5str[33]; - int total_len = 0; - - md5str[32] = '\0'; - - for(int i = 0; i < num; ++i) { - bool has_md5 = false; - if(map->flags & XF_MAP_HAS_MD5) { - if(!(map->flags & XF_MAP_HAS_FILE_LENGTH)) - return XF_E_INVALID_PARAMETER; - - has_md5 = true; - for(int j = 0; j < 16; ++j) { - uint8_t byte = map[i].md5[j]; - md5str[2*j] = hex[(byte >> 4) & 0xf]; - md5str[2*j+1] = hex[byte & 0xf]; - } - } - - int len; - if(!has_md5) { - len = snprintf(buf, sizeof(buf), "%s - %lx %lu\n", - map[i].name, - (unsigned long)map[i].offset, - (unsigned long)map[i].length); - } else { - len = snprintf(buf, sizeof(buf), "%s %s %lu 0x%lx %lu\n", - map[i].name, md5str, - (unsigned long)map[i].file_length, - (unsigned long)map[i].offset, - (unsigned long)map[i].length); - } - - if(len < 0 || (size_t)len >= sizeof(buf)) - return XF_E_LINE_TOO_LONG; - - if(s) { - int rc = xf_stream_write(s, buf, len); - if(rc != len) - return XF_E_IO; - } - - total_len += len; - } - - return total_len; -} diff --git a/lib/x1000-installer/src/xf_nandio.c b/lib/x1000-installer/src/xf_nandio.c deleted file mode 100644 index 6dc87bc420..0000000000 --- a/lib/x1000-installer/src/xf_nandio.c +++ /dev/null @@ -1,292 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2021 Aidan MacDonald - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ - -#include "xf_nandio.h" -#include "xf_error.h" -#include "core_alloc.h" -#include "system.h" -#include <string.h> -#include <stdbool.h> - -int xf_nandio_init(struct xf_nandio* nio) -{ - int rc; - - memset(nio, 0, sizeof(*nio)); - - /* open NAND */ - nio->ndrv = nand_init(); - nand_lock(nio->ndrv); - rc = nand_open(nio->ndrv); - if(rc != NAND_SUCCESS) { - nio->nand_err = rc; - rc = XF_E_NAND; - goto out; - } - - /* read chip parameters */ - nio->page_size = nio->ndrv->chip->page_size; - nio->block_size = nio->page_size << nio->ndrv->chip->log2_ppb; - - /* allocate memory */ - size_t alloc_size = 0; - alloc_size += CACHEALIGN_SIZE - 1; - alloc_size += nio->block_size * 2; - - nio->alloc_handle = core_alloc_ex("xf_nandio", alloc_size, &buflib_ops_locked); - if(nio->alloc_handle < 0) { - rc = XF_E_OUT_OF_MEMORY; - goto out_nclose; - } - - uint8_t* buffer = core_get_data(nio->alloc_handle); - CACHEALIGN_BUFFER(buffer, alloc_size); - - nio->old_buf = buffer; - nio->new_buf = &buffer[nio->block_size]; - - rc = XF_E_SUCCESS; - goto out; - - out_nclose: - nand_close(nio->ndrv); - out: - nand_unlock(nio->ndrv); - return rc; -} - -void xf_nandio_destroy(struct xf_nandio* nio) -{ - nio->alloc_handle = core_free(nio->alloc_handle); - - if(nio->ndrv) { - nand_lock(nio->ndrv); - nand_close(nio->ndrv); - nand_unlock(nio->ndrv); - nio->ndrv = NULL; - } -} - -static bool is_page_blank(const uint8_t* buf, uint32_t length) -{ - for(uint32_t i = 0; i < length; ++i) - if(buf[i] != 0xff) - return false; - - return true; -} - -static int flush_block(struct xf_nandio* nio, bool invalidate) -{ - /* no block, or only reading - flush is a no-op */ - if(!nio->block_valid || nio->mode == XF_NANDIO_READ) - return XF_E_SUCCESS; - - /* nothing to do if new data is same as old data */ - if(!memcmp(nio->old_buf, nio->new_buf, nio->block_size)) - return XF_E_SUCCESS; - - /* data mismatch during verification - report the error */ - if(nio->mode == XF_NANDIO_VERIFY) - return XF_E_VERIFY_FAILED; - - /* erase the block */ - int rc = nand_block_erase(nio->ndrv, nio->cur_block); - if(rc != NAND_SUCCESS) { - nio->block_valid = false; - nio->nand_err = rc; - return XF_E_NAND; - } - - size_t oob_size = nio->ndrv->chip->oob_size; - - unsigned page = 0; - nand_page_t page_addr = nio->cur_block; - for(; page < nio->ndrv->ppb; ++page, ++page_addr) { - /* skip programming blank pages to go faster & reduce wear */ - uint8_t* page_data = &nio->new_buf[page * nio->page_size]; - if(is_page_blank(page_data, nio->page_size)) - continue; - - /* copy page and write blank OOB data */ - memcpy(nio->ndrv->page_buf, page_data, nio->page_size); - memset(&nio->ndrv->page_buf[nio->page_size], 0xff, oob_size); - - /* program the page */ - rc = nand_page_program(nio->ndrv, page_addr, nio->ndrv->page_buf); - if(rc != NAND_SUCCESS) { - nio->block_valid = false; - nio->nand_err = rc; - return XF_E_NAND; - } - } - - if(invalidate) - nio->block_valid = false; - else { - /* update our 'old' buffer so a subsequent flush - * will not reprogram the same block */ - memcpy(nio->old_buf, nio->new_buf, nio->block_size); - } - - return XF_E_SUCCESS; -} - -static int seek_to_block(struct xf_nandio* nio, nand_block_t block_addr, - size_t offset_in_block) -{ - /* already on this block? */ - if(nio->block_valid && block_addr == nio->cur_block) { - nio->offset_in_block = offset_in_block; - return XF_E_SUCCESS; - } - - /* ensure new block is within range */ - if(block_addr >= (nio->ndrv->chip->nr_blocks << nio->ndrv->chip->log2_ppb)) - return XF_E_OUT_OF_RANGE; - - /* flush old block */ - int rc = flush_block(nio, true); - if(rc) - return rc; - - nio->block_valid = false; - - /* read the new block */ - unsigned page = 0; - nand_page_t page_addr = block_addr; - for(; page < nio->ndrv->ppb; ++page, ++page_addr) { - rc = nand_page_read(nio->ndrv, page_addr, nio->ndrv->page_buf); - if(rc != NAND_SUCCESS) { - nio->nand_err = rc; - return XF_E_NAND; - } - - memcpy(&nio->old_buf[page * nio->page_size], nio->ndrv->page_buf, nio->page_size); - } - - /* copy to 2nd buffer */ - memcpy(nio->new_buf, nio->old_buf, nio->block_size); - - /* update position */ - nio->cur_block = block_addr; - nio->offset_in_block = offset_in_block; - nio->block_valid = true; - return XF_E_SUCCESS; -} - -int xf_nandio_set_mode(struct xf_nandio* nio, enum xf_nandio_mode mode) -{ - nand_lock(nio->ndrv); - - /* flush the current block before switching to the new mode, - * to ensure consistency */ - int rc = flush_block(nio, false); - if(rc) - goto err; - - nio->mode = mode; - rc = XF_E_SUCCESS; - - err: - nand_unlock(nio->ndrv); - return rc; -} - -static int nandio_rdwr(struct xf_nandio* nio, void* buf, size_t count, bool write) -{ - while(count > 0) { - void* ptr; - size_t amount = count; - int rc = xf_nandio_get_buffer(nio, &ptr, &amount); - if(rc) - return rc; - - if(write) - memcpy(ptr, buf, amount); - else - memcpy(buf, ptr, amount); - - count -= amount; - } - - return XF_E_SUCCESS; -} - -int xf_nandio_seek(struct xf_nandio* nio, size_t offset) -{ - uint32_t block_nr = offset / nio->block_size; - size_t offset_in_block = offset % nio->block_size; - nand_block_t block_addr = block_nr << nio->ndrv->chip->log2_ppb; - - nand_lock(nio->ndrv); - int rc = seek_to_block(nio, block_addr, offset_in_block); - nand_unlock(nio->ndrv); - - return rc; -} - -int xf_nandio_read(struct xf_nandio* nio, void* buf, size_t count) -{ - return nandio_rdwr(nio, buf, count, false); -} - -int xf_nandio_write(struct xf_nandio* nio, const void* buf, size_t count) -{ - return nandio_rdwr(nio, (void*)buf, count, true); -} - -int xf_nandio_get_buffer(struct xf_nandio* nio, void** buf, size_t* count) -{ - nand_lock(nio->ndrv); - - /* make sure the current block data is read in */ - int rc = seek_to_block(nio, nio->cur_block, nio->offset_in_block); - if(rc) - goto err; - - size_t amount_left = nio->block_size - nio->offset_in_block; - if(amount_left == 0) { - amount_left = nio->block_size; - rc = seek_to_block(nio, nio->cur_block + nio->ndrv->ppb, 0); - if(rc) - goto err; - } - - *buf = &nio->new_buf[nio->offset_in_block]; - *count = MIN(*count, amount_left); - - nio->offset_in_block += *count; - rc = XF_E_SUCCESS; - - err: - nand_unlock(nio->ndrv); - return rc; -} - -int xf_nandio_flush(struct xf_nandio* nio) -{ - nand_lock(nio->ndrv); - int rc = flush_block(nio, false); - nand_unlock(nio->ndrv); - - return rc; -} diff --git a/lib/x1000-installer/src/xf_package.c b/lib/x1000-installer/src/xf_package.c deleted file mode 100644 index fb107aef72..0000000000 --- a/lib/x1000-installer/src/xf_package.c +++ /dev/null @@ -1,261 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2021 Aidan MacDonald - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ - -#include "xf_package.h" -#include "xf_error.h" -#include "pathfuncs.h" -#include "file.h" -#include "core_alloc.h" -#include "md5.h" -#include "system.h" -#include <stdbool.h> -#include <string.h> - -#ifdef ROCKBOX -# include "microtar-rockbox.h" -#else -# include "microtar-stdio.h" -#endif - -#define METADATA_SIZE 4096 /* size of the metadata buffer */ -#define MAX_MAP_SIZE 32 /* maximum number of map entries */ - -static int pkg_alloc(struct xf_package* pkg) -{ - memset(pkg, 0, sizeof(*pkg)); - - /* calculate allocation size */ - size_t alloc_size = 0; - alloc_size += ALIGN_UP_P2(sizeof(mtar_t), 3); - alloc_size += ALIGN_UP_P2(sizeof(struct xf_map) * MAX_MAP_SIZE, 3); - alloc_size += ALIGN_UP_P2(METADATA_SIZE, 3); - alloc_size += 7; /* for alignment */ - - pkg->alloc_handle = core_alloc_ex("xf_package", alloc_size, &buflib_ops_locked); - if(pkg->alloc_handle < 0) - return XF_E_OUT_OF_MEMORY; - - /* distribute memory */ - uint8_t* buf = (uint8_t*)core_get_data(pkg->alloc_handle); - memset(buf, 0, alloc_size); - ALIGN_BUFFER(buf, alloc_size, 8); - - pkg->tar = (mtar_t*)buf; - buf += ALIGN_UP_P2(sizeof(mtar_t), 3); - - pkg->map = (struct xf_map*)buf; - buf += ALIGN_UP_P2(sizeof(struct xf_map) * MAX_MAP_SIZE, 3); - - pkg->metadata = (char*)buf; - buf += ALIGN_UP_P2(METADATA_SIZE, 3); - - return XF_E_SUCCESS; -} - -static int read_meta_line_cb(int n, char* buf, void* arg) -{ - struct xf_package* pkg = (struct xf_package*)arg; - size_t length = strlen(buf); - - /* skip blank lines and the first line (it's reserved for old format) */ - if(n == 0 || length == 0) - return 0; - - /* metadata lines require an '=' sign to separate key and value */ - if(!strchr(buf, '=')) - return XF_E_SYNTAX_ERROR; - - /* we need space to copy the key-value pair plus a null terminator */ - if(length + 1 >= METADATA_SIZE - pkg->metadata_len) - return XF_E_BUF_OVERFLOW; - - memcpy(&pkg->metadata[pkg->metadata_len], buf, length + 1); - pkg->metadata_len += length + 1; - return 0; -} - -static int pkg_read_meta(struct xf_package* pkg) -{ - struct xf_stream stream; - int rc = xf_open_tar(pkg->tar, "bootloader-info.txt", &stream); - if(rc) - return XF_E_CANNOT_OPEN_FILE; - - char buf[200]; - rc = xf_stream_read_lines(&stream, buf, sizeof(buf), read_meta_line_cb, pkg); - xf_stream_close(&stream); - return rc; -} - -static int pkg_read_map(struct xf_package* pkg, - const struct xf_map* dflt_map, int dflt_map_size) -{ - /* Attempt to load and parse the map file */ - struct xf_stream stream; - int rc = xf_open_tar(pkg->tar, "flashmap.txt", &stream); - - /* If the flash map is absent but a default map has been provided, - * then the update is in the old fixed format. */ - if(rc == MTAR_ENOTFOUND && dflt_map) { - if(dflt_map_size > MAX_MAP_SIZE) - return XF_E_INVALID_PARAMETER; - - for(int i = 0; i < dflt_map_size; ++i) { - pkg->map[i] = dflt_map[i]; - pkg->map[i].flags &= ~(XF_MAP_HAS_MD5 | XF_MAP_HAS_FILE_LENGTH); - } - - pkg->map_size = dflt_map_size; - return XF_E_SUCCESS; - } - - if(rc != MTAR_ESUCCESS) - return XF_E_CANNOT_OPEN_FILE; - - rc = xf_map_parse(&stream, pkg->map, MAX_MAP_SIZE); - if(rc < 0) - goto err; - - /* Sort the map; reject it if there is any overlap. */ - pkg->map_size = rc; - if(xf_map_sort(pkg->map, pkg->map_size)) { - rc = XF_E_INVALID_PARAMETER; - goto err; - } - - /* All packages in the 'new' format are required to have MD5 sums. */ - for(int i = 0; i < pkg->map_size; ++i) { - if(!(pkg->map[i].flags & XF_MAP_HAS_MD5) || - !(pkg->map[i].flags & XF_MAP_HAS_FILE_LENGTH)) { - rc = XF_E_VERIFY_FAILED; - goto err; - } - } - - rc = XF_E_SUCCESS; - - err: - xf_stream_close(&stream); - return rc; -} - -static int pkg_verify(struct xf_package* pkg) -{ - struct xf_stream stream; - md5_context ctx; - uint8_t buffer[128]; - - for(int i = 0; i < pkg->map_size; ++i) { - /* At a bare minimum, check that the file exists. */ - int rc = xf_open_tar(pkg->tar, pkg->map[i].name, &stream); - if(rc) - return XF_E_VERIFY_FAILED; - - /* Also check that it isn't bigger than the update region. - * That would normally indicate a problem. */ - off_t streamsize = xf_stream_get_size(&stream); - if(streamsize > (off_t)pkg->map[i].length) { - rc = XF_E_VERIFY_FAILED; - goto err; - } - - /* Check against the listed file length. */ - if(pkg->map[i].flags & XF_MAP_HAS_FILE_LENGTH) { - if(streamsize != (off_t)pkg->map[i].file_length) { - rc = XF_E_VERIFY_FAILED; - goto err; - } - } - - /* Check the MD5 sum if we have it. */ - if(pkg->map[i].flags & XF_MAP_HAS_MD5) { - md5_starts(&ctx); - while(1) { - ssize_t n = xf_stream_read(&stream, buffer, sizeof(buffer)); - if(n < 0) { - rc = XF_E_IO; - goto err; - } - - md5_update(&ctx, buffer, n); - if((size_t)n < sizeof(buffer)) - break; - } - - md5_finish(&ctx, buffer); - if(memcpy(buffer, pkg->map[i].md5, 16)) { - rc = XF_E_VERIFY_FAILED; - goto err; - } - } - - err: - xf_stream_close(&stream); - if(rc) - return rc; - } - - /* All files passed verification */ - return XF_E_SUCCESS; -} - -int xf_package_open_ex(struct xf_package* pkg, const char* file, - const struct xf_map* dflt_map, int dflt_map_size) -{ - int rc = pkg_alloc(pkg); - if(rc) - return rc; - -#ifdef ROCKBOX - rc = mtar_open(pkg->tar, file, O_RDONLY); -#else - rc = mtar_open(pkg->tar, file, "r"); -#endif - if(rc != MTAR_ESUCCESS) { - rc = XF_E_CANNOT_OPEN_FILE; - goto err; - } - - rc = pkg_read_meta(pkg); - if(rc) - goto err; - - rc = pkg_read_map(pkg, dflt_map, dflt_map_size); - if(rc) - goto err; - - rc = pkg_verify(pkg); - if(rc) - goto err; - - err: - if(rc) - xf_package_close(pkg); - return rc; -} - -void xf_package_close(struct xf_package* pkg) -{ - if(mtar_is_open(pkg->tar)) - mtar_close(pkg->tar); - - pkg->alloc_handle = core_free(pkg->alloc_handle); -} diff --git a/lib/x1000-installer/src/xf_stream.c b/lib/x1000-installer/src/xf_stream.c deleted file mode 100644 index b6391b2c8d..0000000000 --- a/lib/x1000-installer/src/xf_stream.c +++ /dev/null @@ -1,211 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2021 Aidan MacDonald - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ - -#include "xf_stream.h" -#include "xf_error.h" -#include "file.h" -#include <stdbool.h> -#include <string.h> -#include <ctype.h> - -/* - * File streams - */ - -static off_t file_stream_get_size(struct xf_stream* s) -{ - return filesize(s->data); -} - -static ssize_t file_stream_read(struct xf_stream* s, void* buf, size_t len) -{ - return read(s->data, buf, len); -} - -static ssize_t file_stream_write(struct xf_stream* s, const void* buf, size_t len) -{ - return write(s->data, buf, len); -} - -static int file_stream_close(struct xf_stream* s) -{ - return close(s->data); -} - -static const struct xf_stream_ops file_stream_ops = { - .xget_size = file_stream_get_size, - .xread = file_stream_read, - .xwrite = file_stream_write, - .xclose = file_stream_close, -}; - -int xf_open_file(const char* file, int flags, struct xf_stream* s) -{ - s->data = open(file, flags, 0666); - s->ops = &file_stream_ops; - return (s->data >= 0) ? 0 : -1; -} - -/* - * Tar streams - */ - -static off_t tar_stream_get_size(struct xf_stream* s) -{ - mtar_t* mtar = (mtar_t*)s->data; - return mtar_get_header(mtar)->size; -} - -static ssize_t tar_stream_read(struct xf_stream* s, void* buffer, size_t count) -{ - mtar_t* mtar = (mtar_t*)s->data; - - int ret = mtar_read_data(mtar, buffer, count); - if(ret < 0) - return -1; - - return ret; -} - -static ssize_t tar_stream_write(struct xf_stream* s, const void* buffer, size_t count) -{ - mtar_t* mtar = (mtar_t*)s->data; - - int ret = mtar_write_data(mtar, buffer, count); - if(ret < 0) - return -1; - - return ret; -} - -static int tar_stream_close(struct xf_stream* s) -{ - mtar_t* mtar = (mtar_t*)s->data; - - if(mtar_access_mode(mtar) == MTAR_WRITE) { - if(mtar_update_file_size(mtar) != MTAR_ESUCCESS) - return -1; - if(mtar_end_data(mtar) != MTAR_ESUCCESS) - return -1; - } - - return 0; -} - -static const struct xf_stream_ops tar_stream_ops = { - .xget_size = tar_stream_get_size, - .xread = tar_stream_read, - .xwrite = tar_stream_write, - .xclose = tar_stream_close, -}; - -int xf_open_tar(mtar_t* mtar, const char* file, struct xf_stream* s) -{ - int err = mtar_find(mtar, file); - if(err != MTAR_ESUCCESS) - return err; - - /* must only read normal files */ - const mtar_header_t* h = mtar_get_header(mtar); - if(h->type != 0 && h->type != MTAR_TREG) - return MTAR_EFAILURE; - - s->data = (intptr_t)mtar; - s->ops = &tar_stream_ops; - return MTAR_ESUCCESS; -} - -int xf_create_tar(mtar_t* mtar, const char* file, size_t size, struct xf_stream* s) -{ - int err = mtar_write_file_header(mtar, file, size); - if(err) - return err; - - s->data = (intptr_t)mtar; - s->ops = &tar_stream_ops; - return MTAR_ESUCCESS; -} - -/* - * Utility functions - */ - -int xf_stream_read_lines(struct xf_stream* s, char* buf, size_t bufsz, - int(*callback)(int n, char* buf, void* arg), void* arg) -{ - char* startp, *endp; - ssize_t bytes_read; - int rc; - - int n = 0; - size_t pos = 0; - bool at_eof = false; - - if(bufsz <= 1) - return XF_E_LINE_TOO_LONG; - - while(!at_eof) { - bytes_read = xf_stream_read(s, &buf[pos], bufsz - pos - 1); - if(bytes_read < 0) - return XF_E_IO; - - /* short read is end of file */ - if((size_t)bytes_read < bufsz - pos - 1) - at_eof = true; - - pos += bytes_read; - buf[pos] = '\0'; - - startp = endp = buf; - while(endp != &buf[pos]) { - endp = strchr(startp, '\n'); - if(endp) { - *endp = '\0'; - } else { - if(!at_eof) { - if(startp == buf) - return XF_E_LINE_TOO_LONG; - else - break; /* read ahead to look for newline */ - } else { - if(startp == &buf[pos]) - break; /* nothing left to do */ - else - endp = &buf[pos]; /* last line missing a newline - - * treat EOF as newline */ - } - } - - rc = callback(n++, startp, arg); - if(rc != 0) - return rc; - - startp = endp + 1; - } - - if(startp <= &buf[pos]) { - memmove(buf, startp, &buf[pos] - startp); - pos = &buf[pos] - startp; - } - } - - return 0; -} diff --git a/lib/x1000-installer/src/xf_update.c b/lib/x1000-installer/src/xf_update.c deleted file mode 100644 index 5a7c3b0430..0000000000 --- a/lib/x1000-installer/src/xf_update.c +++ /dev/null @@ -1,149 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2021 Aidan MacDonald - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ - -#include "xf_update.h" -#include "xf_error.h" -#include "file.h" -#include "md5.h" -#include <string.h> - -static int process_stream(struct xf_nandio* nio, - struct xf_map* map, - struct xf_stream* stream) -{ - void* buffer; - size_t count; - int rc; - - /* calculate MD5 on the fly if taking a backup */ - md5_context md5_ctx; - if(nio->mode == XF_NANDIO_READ) - md5_starts(&md5_ctx); - - /* first deal with the file data */ - size_t bytes_left = map->length; - while(bytes_left > 0) { - count = bytes_left; - rc = xf_nandio_get_buffer(nio, &buffer, &count); - if(rc) - return rc; - - if(nio->mode == XF_NANDIO_READ) { - md5_update(&md5_ctx, buffer, count); - rc = xf_stream_write(stream, buffer, count); - } else { - rc = xf_stream_read(stream, buffer, count); - } - - bytes_left -= count; - - if(rc < 0 || (size_t)rc > count) - return XF_E_IO; - - if((size_t)rc < count) { - /* backup - we could not write all the data */ - if(nio->mode == XF_NANDIO_READ) - return XF_E_IO; - - /* update - clear rest of buffer to 0xff */ - memset(buffer + rc, 0xff, count - rc); - break; - } - } - - /* if updating - write blanks to the remainder of the region */ - while(bytes_left > 0) { - count = bytes_left; - rc = xf_nandio_get_buffer(nio, &buffer, &count); - if(rc) - return rc; - - memset(buffer, 0xff, count); - bytes_left -= count; - } - - /* finalize the MD5 sum */ - if(nio->mode == XF_NANDIO_READ) { - md5_finish(&md5_ctx, map->md5); - map->file_length = map->length; - map->flags |= XF_MAP_HAS_MD5 | XF_MAP_HAS_FILE_LENGTH; - } - - return XF_E_SUCCESS; -} - -static int process_map(struct xf_nandio* nio, struct xf_map* map, int map_size, - xf_update_open_stream_cb open_stream, void* os_arg) -{ - int rc, rc2; - struct xf_stream stream; - - /* ensure the map is sequential and non-overlapping before continuing */ - if(xf_map_validate(map, map_size) != 0) - return XF_E_INVALID_PARAMETER; - - for(int i = 0; i < map_size; ++i) { - /* seek to initial offset */ - rc = xf_nandio_seek(nio, map[i].offset); - if(rc) - return rc; - - rc = open_stream(os_arg, map[i].name, &stream); - if(rc) - return XF_E_CANNOT_OPEN_FILE; - - /* process the stream and be sure to close it even on error */ - rc = process_stream(nio, &map[i], &stream); - rc2 = xf_stream_close(&stream); - - /* bail if either operation raised an error */ - if(rc) - return rc; - if(rc2) - return rc2; - } - - /* now flush to ensure all data was written */ - rc = xf_nandio_flush(nio); - if(rc) - return rc; - - return XF_E_SUCCESS; -} - -static const enum xf_nandio_mode update_mode_to_nandio[] = { - [XF_UPDATE] = XF_NANDIO_PROGRAM, - [XF_BACKUP] = XF_NANDIO_READ, - [XF_VERIFY] = XF_NANDIO_VERIFY, -}; - -int xf_updater_run(enum xf_update_mode mode, struct xf_nandio* nio, - struct xf_map* map, int map_size, - xf_update_open_stream_cb open_stream, void* arg) -{ - /* Switch NAND I/O into the correct mode */ - int rc = xf_nandio_set_mode(nio, update_mode_to_nandio[mode]); - if(rc) - return rc; - - /* This does all the real work */ - return process_map(nio, map, map_size, open_stream, arg); -} |