Skip to content

Commit

Permalink
[nrf mergeup] Merge upstream up to commit 84b56b6
Browse files Browse the repository at this point in the history
Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
  • Loading branch information
bjarki-andreasen committed Sep 27, 2024
2 parents e919a06 + 84b56b6 commit 5901ad2
Show file tree
Hide file tree
Showing 95 changed files with 1,987 additions and 581 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/imgtool.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
branches:
- main
- v*-branch
pull_request:

name: imgtool

Expand All @@ -11,7 +12,38 @@ concurrency:
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.x", "pypy3.9"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: 'pipenv'
cache-dependency-path: |
scripts/setup.py
- name: Install dependencies
run: |
pip install --user pipenv
- name: Run tests
run: |
cd scripts
pipenv run pip install pytest -e .
pipenv run pytest --junitxml=../junit/pytest-results-${{ matrix.python-version }}.xml
- name: Upload test results
uses: actions/upload-artifact@v3
if: always()
with:
name: pytest-results-${{ matrix.python-version }}
path: |
junit/pytest-results-${{ matrix.python-version }}*.xml
if-no-files-found: ignore
environment:
if: ${{ github.event_name == 'push' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/zephyr_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
./scripts/twister --inline-logs -v -N -M --integration --overflow-as-errors --retry-failed 2 ${test_paths}
- name: Upload Tests Results
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: always()
with:
name: Tests Results
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = ["sim"]
exclude = ["ptest"]
resolver = "2"

# The simulator runs very slowly without optimization. A value of 1
# compiles in about half the time, but runs about 5-6 times slower. 2
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ Developers are welcome!
Use the following links to join or see more about the project:

* [Our developer mailing list](https://groups.io/g/MCUBoot)
* [Our Slack channel](https://mcuboot.slack.com/) <br />
Get [your invite](https://join.slack.com/t/mcuboot/shared_invite/MjE2NDcwMTQ2MTYyLTE1MDA4MTIzNTAtYzgyZTU0NjFkMg)
* [Our Discord channel](https://discord.com/channels/1106321706588577904/1106322802308550716) <br />
Get [your invite](https://discord.com/invite/5PpXhvda5p)
176 changes: 163 additions & 13 deletions boot/boot_serial/src/boot_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,28 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
#else
#define BOOT_SERIAL_HASH_SIZE_MAX 0
#endif
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
#define BOOT_SERIAL_SLOT_INFO_SIZE_MAX 164
#else
#define BOOT_SERIAL_SLOT_INFO_SIZE_MAX 0
#endif

#if (128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + BOOT_SERIAL_HASH_SIZE_MAX) > \
BOOT_SERIAL_SLOT_INFO_SIZE_MAX
#define BOOT_SERIAL_MAX_MESSAGE_SIZE (128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + \
BOOT_SERIAL_HASH_SIZE_MAX)
#else
#define BOOT_SERIAL_MAX_MESSAGE_SIZE BOOT_SERIAL_SLOT_INFO_SIZE_MAX
#endif

#define BOOT_SERIAL_OUT_MAX ((128 + BOOT_SERIAL_IMAGE_STATE_SIZE_MAX + \
BOOT_SERIAL_HASH_SIZE_MAX) * BOOT_IMAGE_NUMBER)
#define BOOT_SERIAL_OUT_MAX (BOOT_SERIAL_MAX_MESSAGE_SIZE * BOOT_IMAGE_NUMBER)

#define BOOT_SERIAL_FRAME_MTU 124 /* 127 - pkt start (2 bytes) and stop (1 byte) */

/* Number of estimated CBOR elements for responses */
#define CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP 4
#define CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP 3

#ifdef __ZEPHYR__
/* base64 lib encodes data to null-terminated string */
#define BASE64_ENCODE_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1)
Expand Down Expand Up @@ -259,11 +275,7 @@ bs_list(char *buf, int len)
int swap_status = boot_swap_type_multi(image_index);
#endif

#ifdef MCUBOOT_SINGLE_APPLICATION_SLOT
for (slot = 0; slot < 1; slot++) {
#else
for (slot = 0; slot < 2; slot++) {
#endif
for (slot = 0; slot < MCUBOOT_IMAGE_NUMBER; slot++) {
FIH_DECLARE(fih_rc, FIH_FAILURE);
uint8_t tmpbuf[64];

Expand Down Expand Up @@ -576,6 +588,139 @@ bs_list_set(uint8_t op, char *buf, int len)
}
}

#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
static void
bs_slot_info(uint8_t op, char *buf, int len)
{
uint32_t slot, area_id;
const struct flash_area *fap;
uint8_t image_index = 0;
int rc;
bool ok = true;
const struct image_max_size *image_max_sizes;

if (op != NMGR_OP_READ) {
bs_rc_rsp(MGMT_ERR_ENOTSUP);
}

image_max_sizes = boot_get_max_app_size();

zcbor_map_start_encode(cbor_state, 1);
zcbor_tstr_put_lit_cast(cbor_state, "images");
zcbor_list_start_encode(cbor_state, MCUBOOT_IMAGE_NUMBER);

IMAGES_ITER(image_index) {
for (slot = 0; slot < MCUBOOT_IMAGE_NUMBER; slot++) {
if (slot == 0) {
ok = zcbor_map_start_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP) &&
zcbor_tstr_put_lit(cbor_state, "image") &&
zcbor_uint32_put(cbor_state, (uint32_t)image_index) &&
zcbor_tstr_put_lit(cbor_state, "slots") &&
zcbor_list_start_encode(cbor_state, MCUBOOT_IMAGE_NUMBER);

if (!ok) {
goto finish;
}
}

ok = zcbor_map_start_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP) &&
zcbor_tstr_put_lit(cbor_state, "slot") &&
zcbor_uint32_put(cbor_state, slot);

if (!ok) {
goto finish;
}

area_id = flash_area_id_from_multi_image_slot(image_index, slot);
rc = flash_area_open(area_id, &fap);

if (rc) {
ok = zcbor_tstr_put_lit(cbor_state, "rc") &&
zcbor_int32_put(cbor_state, rc);
} else {
if (sizeof(fap->fa_size) == sizeof(uint64_t)) {
ok = zcbor_tstr_put_lit(cbor_state, "size") &&
zcbor_uint64_put(cbor_state, fap->fa_size);
} else {
ok = zcbor_tstr_put_lit(cbor_state, "size") &&
zcbor_uint32_put(cbor_state, fap->fa_size);
}

if (!ok) {
flash_area_close(fap);
goto finish;
}

/*
* Check if we support uploading to this slot and if so, return the
* image ID
*/
#if defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
zcbor_uint32_put(cbor_state, (image_index + 1));
#elif defined(MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD)
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
zcbor_uint32_put(cbor_state, (image_index * 2 + slot + 1));
#else
if (slot == 1) {
ok = zcbor_tstr_put_lit(cbor_state, "upload_image_id") &&
zcbor_uint32_put(cbor_state, (image_index * 2 + 1));
}
#endif

flash_area_close(fap);

if (!ok) {
goto finish;
}

ok = zcbor_map_end_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_SLOTS_MAP);

if (!ok) {
goto finish;
}

if (slot == (MCUBOOT_IMAGE_NUMBER - 1)) {
ok = zcbor_list_end_encode(cbor_state, MCUBOOT_IMAGE_NUMBER);

if (!ok) {
goto finish;
}

if (image_max_sizes[image_index].calculated == true) {
ok = zcbor_tstr_put_lit(cbor_state, "max_image_size") &&
zcbor_uint32_put(cbor_state,
image_max_sizes[image_index].max_size);

if (!ok) {
goto finish;
}
}

ok = zcbor_map_end_encode(cbor_state, CBOR_ENTRIES_SLOT_INFO_IMAGE_MAP);

}
}

if (!ok) {
goto finish;
}
}
}

ok = zcbor_list_end_encode(cbor_state, MCUBOOT_IMAGE_NUMBER) &&
zcbor_map_end_encode(cbor_state, 1);

finish:
if (!ok) {
reset_cbor_state();
bs_rc_rsp(MGMT_ERR_ENOMEM);
}

boot_serial_output();
}
#endif

#ifdef MCUBOOT_ERASE_PROGRESSIVELY
/** Erases range of flash, aligned to sector size
*
Expand Down Expand Up @@ -611,7 +756,7 @@ static off_t erase_range(const struct flash_area *fap, off_t start, off_t end)
}

size = flash_sector_get_off(&sect) + flash_sector_get_size(&sect) - start;
BOOT_LOG_INF("Erasing range 0x%jx:0x%jx", (intmax_t)start,
BOOT_LOG_DBG("Erasing range 0x%jx:0x%jx", (intmax_t)start,
(intmax_t)(start + size - 1));

rc = flash_area_erase(fap, start, size);
Expand Down Expand Up @@ -796,7 +941,7 @@ bs_upload(char *buf, int len)
rem_bytes = 0;
}

BOOT_LOG_INF("Writing at 0x%x until 0x%x", curr_off, curr_off + img_chunk_len);
BOOT_LOG_DBG("Writing at 0x%x until 0x%x", curr_off, curr_off + (uint32_t)img_chunk_len);
/* Write flash aligned chunk, note that img_chunk_len now holds aligned length */
#if defined(MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE) && MCUBOOT_SERIAL_UNALIGNED_BUFFER_SIZE > 0
if (flash_area_align(fap) > 1 &&
Expand Down Expand Up @@ -870,7 +1015,7 @@ bs_upload(char *buf, int len)
}

out:
BOOT_LOG_INF("RX: 0x%x", rc);
BOOT_LOG_DBG("RX: 0x%x", rc);
zcbor_map_start_encode(cbor_state, 10);
zcbor_tstr_put_lit_cast(cbor_state, "rc");
zcbor_int32_put(cbor_state, rc);
Expand Down Expand Up @@ -1019,6 +1164,11 @@ boot_serial_input(char *buf, int len)
case IMGMGR_NMGR_ID_UPLOAD:
bs_upload(buf, len);
break;
#ifdef MCUBOOT_SERIAL_IMG_GRP_SLOT_INFO
case IMGMGR_NMGR_ID_SLOT_INFO:
bs_slot_info(hdr->nh_op, buf, len);
break;
#endif
default:
bs_rc_rsp(MGMT_ERR_ENOTSUP);
break;
Expand Down Expand Up @@ -1124,7 +1274,7 @@ boot_serial_output(void)
boot_uf->write("\n", 1);
}

BOOT_LOG_INF("TX");
BOOT_LOG_DBG("TX");
}

/*
Expand All @@ -1133,7 +1283,7 @@ boot_serial_output(void)
static int
boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
{
int rc;
size_t rc;
uint16_t crc;
uint16_t len;

Expand All @@ -1145,7 +1295,7 @@ boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
}
#elif __ESPRESSIF__
int err;
err = base64_decode((unsigned char *)&out[*out_off], maxout - *out_off, (size_t *)&rc, (unsigned char *)in, inlen);
err = base64_decode((unsigned char *)&out[*out_off], maxout - *out_off, &rc, (unsigned char *)in, inlen);
if (err) {
return -1;
}
Expand Down
19 changes: 6 additions & 13 deletions boot/boot_serial/src/boot_serial_encryption.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ boot_image_validate_encrypted(const struct flash_area *fa_p,
memset(&boot_data, 0, sizeof(struct boot_loader_state));
image_index = BOOT_CURR_IMG(state);
if(IS_ENCRYPTED(hdr)) {
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fa_p, bs);
if (rc < 0) {
FIH_RET(fih_rc);
}
rc = flash_area_id_to_multi_image_slot(image_index, flash_area_get_id(fa_p));
rc = boot_enc_load(BOOT_CURR_ENC(state), 1, hdr, fa_p, bs);
if (rc < 0) {
FIH_RET(fih_rc);
}
Expand Down Expand Up @@ -129,10 +125,11 @@ decrypt_region_inplace(struct boot_loader_state *state,
size_t blk_off;
uint16_t idx;
uint32_t blk_sz;
uint8_t image_index;

int slot = flash_area_id_to_multi_image_slot(BOOT_CURR_IMG(state),
flash_area_get_id(fap));
uint8_t buf[sz] __attribute__((aligned));
assert(sz <= sizeof buf);
assert(slot >= 0);

bytes_copied = 0;
while (bytes_copied < sz) {
Expand All @@ -147,7 +144,6 @@ decrypt_region_inplace(struct boot_loader_state *state,
return BOOT_EFLASH;
}

image_index = BOOT_CURR_IMG(state);
if (IS_ENCRYPTED(hdr)) {
blk_sz = chunk_sz;
idx = 0;
Expand Down Expand Up @@ -175,7 +171,7 @@ decrypt_region_inplace(struct boot_loader_state *state,
blk_sz = tlv_off - (off + bytes_copied);
}
}
boot_encrypt(BOOT_CURR_ENC(state), image_index, fap,
boot_encrypt(BOOT_CURR_ENC(state), slot,
(off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
blk_off, &buf[idx]);
}
Expand Down Expand Up @@ -222,7 +218,6 @@ decrypt_image_inplace(const struct flash_area *fa_p,
size_t sect_size;
size_t sect_count;
size_t sect;
uint8_t image_index;
struct flash_sector sector;

memset(&boot_data, 0, sizeof(struct boot_loader_state));
Expand All @@ -232,8 +227,6 @@ decrypt_image_inplace(const struct flash_area *fa_p,
rc = flash_area_get_sector(fa_p, boot_status_off(fa_p), &sector);


image_index = BOOT_CURR_IMG(state);

if(IS_ENCRYPTED(hdr)) {
#if 0 //Skip this step?, the image will just not boot if it's not decrypted properly
static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Expand All @@ -245,7 +238,7 @@ decrypt_image_inplace(const struct flash_area *fa_p,
#endif
memset(&boot_data, 0, sizeof(struct boot_loader_state));
/* Load the encryption keys into cache */
rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fa_p, bs);
rc = boot_enc_load(BOOT_CURR_ENC(state), 0, hdr, fa_p, bs);
if (rc < 0) {
FIH_RET(fih_rc);
}
Expand Down
1 change: 1 addition & 0 deletions boot/boot_serial/src/boot_serial_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct nmgr_hdr {
*/
#define IMGMGR_NMGR_ID_STATE 0
#define IMGMGR_NMGR_ID_UPLOAD 1
#define IMGMGR_NMGR_ID_SLOT_INFO 6

void boot_serial_input(char *buf, int len);
extern const struct boot_uart_funcs *boot_uf;
Expand Down
Loading

0 comments on commit 5901ad2

Please sign in to comment.