---
title: "Storage"
description: "Generic block storage over flash, eMMC, or SD, with a portable inline-AES surface and an Alif SecAES key-provisioning extension."
source: https://docs.alplab.ai/sdk/api/storage
---

# `<alp/storage.h>` — Block Storage

Generic block storage over flash, eMMC, or SD, with a portable inline-AES surface and an Alif SecAES key-provisioning extension.

:::danger[Breaking change — writes need `allow_unsafe_write` on Yocto/Linux]
`alp_storage_config_t` now carries `bool allow_unsafe_write`, defaulting to **`false`**. On the Yocto/Linux backend, `alp_storage_write()` and `alp_storage_erase()` return `ALP_ERR_INVAL` unless it is set `true`.

This is a **data-destruction guard**, not a formality. On this backend `/dev/mmcblk<N>` is the **whole-disk** node — not a partition — and on V2N `/dev/mtd0` is the **FIP/bootloader** partition. An unguarded write to either **bricks the board**. Reads are unaffected.
:::

## Header

```c
#include <alp/storage.h>
```

## Read / write a block

```c
alp_storage_t *st = alp_storage_open(&(alp_storage_config_t){
    .kind = ALP_STORAGE_KIND_OSPI_FLASH,
});

uint8_t block[256];
alp_storage_read(st, /* offset */ 0x1000, block, sizeof(block));
alp_storage_write(st, 0x1000, block, sizeof(block));

alp_storage_close(st);
```

### Read-only handles

A handle opened with `read_only = true` refuses writes and erases with `ALP_ERR_NOT_READY`. Since v0.17.0 `alp_storage_erase()` reports it that way too (it previously documented `ALP_ERR_INVAL` for that case), matching `alp_storage_write()`; `ALP_ERR_INVAL` from erase now means misalignment or the Yocto safety gate below.

## `allow_unsafe_write` — the destructive-write guard

| Field                | Type   | Default | Notes                                                                 |
|----------------------|--------|---------|------------------------------------------------------------------------|
| `allow_unsafe_write` | `bool` | `false` | Yocto/Linux backend: `alp_storage_write()` and `alp_storage_erase()` return `ALP_ERR_INVAL` unless `true`. |

```c
alp_storage_config_t cfg = ALP_STORAGE_CONFIG_DEFAULT(ALP_STORAGE_KIND_SD_MMC);
cfg.allow_unsafe_write = true;   // I accept that this node is the whole disk
```

The device nodes this backend opens are not partitions:

- `ALP_STORAGE_KIND_SD_MMC` → `/dev/mmcblk<N>`, the **whole-disk** node. A write at offset 0 lands on the partition table.
- On V2N, `/dev/mtd0` is the **FIP/bootloader** partition. Writing or erasing it without knowing exactly what you are doing bricks the board.

There is no undo and no confirmation prompt on an embedded target, so the SDK requires the caller to opt in per handle. Leave it `false` for read-only workloads.

## Yocto / Linux (V2N / V2M) backend

`alp_storage_open()`, `alp_storage_read()` and `alp_storage_get_info()` are now **real** on this backend; they previously returned `ALP_ERR_NOSUPPORT`.

| Kind                                                          | Node             | Geometry source                    |
|---------------------------------------------------------------|------------------|-------------------------------------|
| `ALP_STORAGE_KIND_SD_MMC`                                     | `/dev/mmcblk<N>` | `BLKGETSIZE64` / `BLKSSZGET`        |
| `INTERNAL_FLASH` / `QSPI_FLASH` / `OSPI_FLASH`                | `/dev/mtd<N>`    | `MEMGETINFO`                        |

Geometry is read from the kernel, not assumed, and I/O is real `pread` / `pwrite`.

`alp_storage_configure_inline_aes()` remains `ALP_ERR_NOSUPPORT` on this backend.

## Inline AES

`alp_storage_configure_inline_aes(storage, &cfg)` is the portable surface for on-the-fly encryption of an open storage device:

```c
static const uint8_t key[16] = { /* AES-128 key */ };
static const uint8_t iv[16]  = { /* XTS tweak */ };

alp_status_t s = alp_storage_configure_inline_aes(st, &(alp_storage_aes_config_t){
    .mode      = ALP_STORAGE_AES_XTS,   /* or ALP_STORAGE_AES_CTR; ALP_STORAGE_AES_OFF disables */
    .key       = key,
    .key_bytes = 16,                    /* 16 / 24 / 32 -> AES-128 / 192 / 256 */
    .iv        = iv,
    .iv_bytes  = 16,
});
```

:::warning[Not implemented by any shipped backend]
In v0.17.0 every storage backend — Zephyr flash, littlefs, Yocto and the software fallback — returns `ALP_ERR_NOSUPPORT` from `alp_storage_configure_inline_aes()`. The surface is declared so code compiles everywhere; a vendor backend that implements it would win on priority. Handle `ALP_ERR_NOSUPPORT`.
:::

### Alif SecAES key provisioning (`<alp/ext/alif/storage.h>`)

On the Alif E8, `alp_alif_storage_secaes_key_provision(s, key, key_bytes)` loads a key into the OSPI SecAES engine through the Secure Enclave. It has a real implementation since v0.17.0, gated on `CONFIG_ALP_SDK_STORAGE_ALIF_SECAES` — **default off, and unverified on silicon**.

- `key_bytes` must be `16` (AES-128 is the only width the SE write-key service accepts); anything else is `ALP_ERR_INVAL`.
- It blocks up to about 35 s for the SE round-trip.
- `ALP_ERR_NOSUPPORT` when the option is off; `ALP_ERR_NOT_READY` (retry-safe) when the handle is closing, the SE is busy, or the transport timed out; `ALP_ERR_IO` on a transport fault, a key-load fault, or a NACK (the SE firmware may not implement the service).
- E4 and E6 have the same key-load fabric, but whether their SE firmware implements the service is untried.

`alp_alif_storage_secaes_get_status()` still returns `ALP_ERR_NOSUPPORT`: Alif has not published an SE service that reads the engine's status back.

## File systems

For higher-level access, declare your partitions in `board.yaml`'s [`storage:` block](https://docs.alplab.ai/sdk/board-yaml#storage-block) — the orchestrator emits the `fixed-partitions` DTS overlay and the per-fs Kconfig (`CONFIG_FILE_SYSTEM_LITTLEFS=y` / `_FAT_FILESYSTEM_ELM=y` / `_FILE_SYSTEM_EXT2=y`) plus an optional static `fs_mount_t alp_storage_mounts[]` table for boot-time iteration.

## Default-initialiser macro

`ALP_STORAGE_CONFIG_DEFAULT(id)` (v0.10) takes the backing-store **kind** as its identity argument:

```c
alp_storage_config_t cfg = ALP_STORAGE_CONFIG_DEFAULT(ALP_STORAGE_KIND_INTERNAL_FLASH);
```

It sets `kind` from `id` plus: `instance_id = 0`, `freq_hz = 0` (use the devicetree default), `read_only = false`, and `allow_unsafe_write = false`.

The `allow_unsafe_write = false` default is the point of the macro here: a config that picks up the new field automatically defaults to the **non-destructive** setting, so code written before the field existed cannot start erasing a bootloader by accident.

See [the shared macro contract](https://docs.alplab.ai/sdk/api/peripheral#default-initialiser-macros) for the C++ compound-literal caveat that applies to every `ALP_*_CONFIG_DEFAULT`.

## See also

- [`<alp/security.h>`](https://docs.alplab.ai/sdk/api/security)
- [`board.yaml`: `storage:` block](https://docs.alplab.ai/sdk/board-yaml#storage-block)
