zephyr/samples/boards/esp32/flash_encryption/src/main.c
Marek Matej 6b57b3b786 soc: xtensa,riscv: esp32xx: refactor folder structure
Refactor the ESP32 target SOCs together with
all related boards. Most braking changes includes:

- changing the CONFIG_SOC_ESP32* to refer to
  the actual soc line (esp32,esp32s2,esp32s3,esp32c3)
- replacing CONFIG_SOC with the CONFIG_SOC_SERIES
- creating CONFIG_SOC_FAMILY_ESP32 to embrace all
  the ESP32 across all used architectures
- introducing CONFIG_SOC_PART_NUMBER_* to
  provide a SOC model config
- introducing the 'common' folder to hide all
  commonly used configs and files.
- updating west.yml to reflect previous changes in hal

Signed-off-by: Marek Matej <marek.matej@espressif.com>
2023-07-25 18:12:33 +02:00

57 lines
1.5 KiB
C

/*
* Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/device.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/storage/flash_map.h>
#include <esp_spi_flash.h>
#include <soc.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(flash_encryption, CONFIG_LOG_DEFAULT_LEVEL);
#if !defined(CONFIG_SOC_SERIES_ESP32)
#error Flash encryption feature is only available for ESP32 SOC yet.
#endif
int main(void)
{
uint8_t buffer[32];
const struct device *flash_device;
off_t address = FIXED_PARTITION_OFFSET(storage_partition);
flash_device = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
if (!device_is_ready(flash_device)) {
printk("%s: device not ready.\n", flash_device->name);
return 0;
}
for (int k = 0; k < 32; k++) {
buffer[k] = k;
}
LOG_HEXDUMP_INF(buffer, sizeof(buffer), "WRITE BUFFER CONTENT");
/* erase flash content */
flash_erase(flash_device, address, 4096);
/* this writes encrypted data into flash */
flash_write(flash_device, address, &buffer, sizeof(buffer));
/* read flash content without decrypting content */
memset(buffer, 0, sizeof(buffer));
spi_flash_read(address, &buffer, sizeof(buffer));
LOG_HEXDUMP_INF(buffer, sizeof(buffer), "FLASH RAW DATA (Encrypted)");
/* read flash content and decrypt */
memset(buffer, 0, sizeof(buffer));
flash_read(flash_device, address, &buffer, sizeof(buffer));
LOG_HEXDUMP_INF(buffer, sizeof(buffer), "FLASH DECRYPTED DATA");
return 0;
}