|
| 1 | +/* unit-common.c |
| 2 | + * |
| 3 | + * Unit test common tools |
| 4 | + * |
| 5 | + * |
| 6 | + * Copyright (C) 2023 wolfSSL Inc. |
| 7 | + * |
| 8 | + * This file is part of wolfBoot. |
| 9 | + * |
| 10 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation; either version 2 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * wolfBoot is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 23 | + */ |
| 24 | +#include <stdint.h> |
| 25 | +#include <stdio.h> |
| 26 | +#include <string.h> |
| 27 | +#include <check.h> |
| 28 | +#include "target.h" |
| 29 | +#define FLASH_SIZE (33 * 1024) |
| 30 | + |
| 31 | +/* Emulation of external flash with a static buffer of 32KB (update) + 1KB (swap) */ |
| 32 | +uint8_t flash[FLASH_SIZE]; |
| 33 | + |
| 34 | +/* Flash lock/unlock counter */ |
| 35 | +static int elocked = 0; |
| 36 | + |
| 37 | +#ifndef BACKUP_MOCKED |
| 38 | + |
| 39 | +uint8_t image_backup(uint8_t part_id) |
| 40 | +{ |
| 41 | + printf("Called image_backup\n"); |
| 42 | + return 0xFF; /* PART_NONE */ |
| 43 | +} |
| 44 | +#endif |
| 45 | + |
| 46 | +#ifndef EXT_MOCKED |
| 47 | + |
| 48 | +/* Mocks for ext_flash_read, ext_flash_write, and ext_flash_erase functions */ |
| 49 | +int ext_flash_read(uintptr_t address, uint8_t *data, int len) { |
| 50 | + printf("Called ext_flash_read %p %p %d\n", address, data, len); |
| 51 | + |
| 52 | + /* Check that the read address and size are within the bounds of the flash memory */ |
| 53 | + ck_assert_int_le(address + len, FLASH_SIZE); |
| 54 | + |
| 55 | + /* Copy the data from the flash memory to the output buffer */ |
| 56 | + memcpy(data, &flash[address], len); |
| 57 | + |
| 58 | + printf("Return value: %d\n", len); |
| 59 | + return len; |
| 60 | +} |
| 61 | + |
| 62 | +int ext_flash_write(uintptr_t address, const uint8_t *data, int len) { |
| 63 | + printf("Called ext_flash_write %p %p %d\n", address, data, len); |
| 64 | + |
| 65 | + |
| 66 | + /* Check that the write address and size are within the bounds of the flash memory */ |
| 67 | + ck_assert_int_le(address + len, FLASH_SIZE); |
| 68 | + |
| 69 | + /* Copy the data from the input buffer to the flash memory */ |
| 70 | + memcpy(&flash[address], data, len); |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +int ext_flash_erase(uintptr_t address, int len) { |
| 76 | + /* Check that the erase address and size are within the bounds of the flash memory */ |
| 77 | + ck_assert_int_le(address + len, FLASH_SIZE); |
| 78 | + |
| 79 | + /* Check that address is aligned to WOLFBOOT_SECTOR_SIZE */ |
| 80 | + ck_assert_int_eq(address, address & ~(WOLFBOOT_SECTOR_SIZE - 1)); |
| 81 | + |
| 82 | + /* Check that len is aligned to WOLFBOOT_SECTOR_SIZE */ |
| 83 | + ck_assert_int_eq(len, len & ~(WOLFBOOT_SECTOR_SIZE - 1)); |
| 84 | + |
| 85 | + |
| 86 | + /* Erase the flash memory by setting each byte to 0xFF, WOLFBOOT_SECTOR_SIZE bytes at a time */ |
| 87 | + uint32_t i; |
| 88 | + for (i = address; i < address + len; i += WOLFBOOT_SECTOR_SIZE) { |
| 89 | + memset(&flash[i], 0xFF, WOLFBOOT_SECTOR_SIZE); |
| 90 | + } |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +void ext_flash_unlock(void) |
| 96 | +{ |
| 97 | + fail_unless(elocked, "Double ext unlock detected\n"); |
| 98 | + elocked--; |
| 99 | +} |
| 100 | +void ext_flash_lock(void) |
| 101 | +{ |
| 102 | + fail_if(elocked, "Double ext lock detected\n"); |
| 103 | + elocked++; |
| 104 | +} |
| 105 | + |
| 106 | +#endif /* EXT_MOCKED */ |
0 commit comments