Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Nordic Semiconductor ASA |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include <zephyr.h> |
| 8 | #include <drivers/flash.h> |
| 9 | #include <mgmt/mcumgr/zephyr_groups.h> |
| 10 | |
| 11 | #include <flash_map_backend/flash_map_backend.h> |
| 12 | #include <sysflash/sysflash.h> |
| 13 | |
| 14 | #include "bootutil/bootutil_log.h" |
| 15 | #include "../boot_serial/src/boot_serial_priv.h" |
| 16 | #include "../boot_serial/src/cbor_encode.h" |
| 17 | |
| 18 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 19 | |
| 20 | int bs_peruser_system_specific(const struct nmgr_hdr *hdr, const char *buffer, |
| 21 | int len, cbor_state_t *cs) |
| 22 | { |
Dominik Ermel | 97b4c79 | 2021-06-25 17:32:38 +0000 | [diff] [blame] | 23 | int mgmt_rc = MGMT_ERR_ENOTSUP; |
| 24 | int rc = -ENOTSUP; |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 25 | |
Dominik Ermel | 97b4c79 | 2021-06-25 17:32:38 +0000 | [diff] [blame] | 26 | if (hdr->nh_group == ZEPHYR_MGMT_GRP_BASE && |
| 27 | hdr->nh_id == ZEPHYR_MGMT_GRP_BASIC_CMD_ERASE_STORAGE && |
| 28 | hdr->nh_op == NMGR_OP_WRITE) { |
| 29 | |
| 30 | const struct flash_area *fa; |
| 31 | |
| 32 | rc = flash_area_open(FLASH_AREA_ID(storage), &fa); |
| 33 | |
| 34 | if (rc < 0) { |
| 35 | LOG_ERR("failed to open flash area"); |
| 36 | } else { |
| 37 | rc = flash_area_erase(fa, 0, FLASH_AREA_SIZE(storage)); |
| 38 | if (rc < 0) { |
| 39 | LOG_ERR("failed to erase flash area"); |
| 40 | } |
| 41 | flash_area_close(fa); |
| 42 | } |
| 43 | if (rc == 0) { |
| 44 | mgmt_rc = MGMT_ERR_OK; |
| 45 | } else { |
| 46 | mgmt_rc = MGMT_ERR_EUNKNOWN; |
| 47 | } |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | map_start_encode(cs, 10); |
| 51 | tstrx_put(cs, "rc"); |
Dominik Ermel | 97b4c79 | 2021-06-25 17:32:38 +0000 | [diff] [blame] | 52 | uintx32_put(cs, mgmt_rc); |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 53 | map_end_encode(cs, 10); |
| 54 | |
| 55 | return rc; |
| 56 | } |