Skip to content

Commit 9d340e3

Browse files
committed
Allow provisioning ESP32 nvs partition when building
Adds the option to provision nvs partition data at build time for the esp32 platform. If the file `nvs_partition.csv` is found in the AtomVM/src/platforms/esp32 directory when building; the nvs partition will be created and added to the `idf.py flash` task as well as to the configuration for the mkimage.sh script in the build directory. An example file is provided and `nvs_partition.csv` is added to `.gitignore` to prevent accidentally pushing secrets to remote repositories. Signed-off-by: Winford <[email protected]>
1 parent 2f33287 commit 9d340e3

File tree

8 files changed

+132
-1
lines changed

8 files changed

+132
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5555
- Added `supervisor:which_children/1`
5656
- Added `monitored_by` in `process_info/2`
5757
- Added ESP32 `-DATOMVM_ELIXIR_SUPPORT=on` configuration option
58+
- Added support for ESP32 development builds to include NVS partition data at build time
5859

5960
### Changed
6061

doc/src/build-instructions.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,71 @@ partitions and the flash partitions layout see [Flash Layout](#flash-layout) bel
304304
305305
To build a single binary image file see [Building a Release Image](#building-a-release-image), below.
306306
307+
#### NVS Partition Provisioning
308+
309+
For streamlining deployment of images for an environment developers may pre-provision NVS partition
310+
data. This is done by creating a file in the AtomVM/src/platforms/esp32 directory named
311+
`nvs_partition.csv`, an example called `nvs_partition.csv-example` is provided in the same
312+
directory. If this file exists it will be included by the mkimage.sh script in the build directory.
313+
The partition is not included in the `idf.py flash` task so that settings made by applications can
314+
be retained. To update changes or restore to the defaults defined in `nvs_partition.csv` delete the
315+
generated `build/nvs.bin` file (if present) and execute the command `idf.py nvs-flash`.
316+
317+
This is a more detailed example, with explanations of the structure:
318+
319+
```{csv}
320+
key,type,encoding,value
321+
network,namespace,,
322+
ssid,data,binary,"NETWORK_NAME"
323+
psk,data,binary,"PASSWORD"
324+
settings,namespace,,
325+
feature0,data,binary,"1"
326+
extra_feature,data,binary,"0"
327+
token,file,binary,/path/to/file
328+
```
329+
330+
Let's break this down line by line:
331+
332+
```csv
333+
key,type,encoding,value
334+
```
335+
This is the header describing the columns. It is important that there is no whitespace at the end of each line
336+
and none separating the commas (`,`) throughout this file.
337+
338+
```csv
339+
network,namespace,,
340+
```
341+
The first entry should have a "key" name and have type "namespace". The namespaces are the same
342+
used to look up the keys with
343+
[esp:nvs_get_binary/2 (or /3)](./apidocs/erlang/eavmlib/esp.md#nvs_get_binary2). Note that the
344+
`encoding` and `value` are empty.
345+
346+
```csv
347+
ssid,data,binary,"NETWORK_NAME"
348+
...
349+
```
350+
The keys must use encoding type `binary` as this is the only type currently supported by AtomVM.
351+
352+
```csv
353+
settings,namespace,,
354+
...
355+
```
356+
Multiple namespaces may be used for separation, followed by their keys.
357+
358+
```csv
359+
token,file,binary,/path/to/file
360+
```
361+
External file contents may be included
362+
363+
The initial values flashed to the `nvs` partition may be changed by applications using
364+
[esp:nvs_put_binary/3](./apidocs/erlang/eavmlib/esp.md#nvs_put_binary3). If you wish to make
365+
changes to the partition data and re-flash without rebuilding and flashing the entire AtomVM build
366+
you may delete the generated `build/nvs.bin` file and run `idf.py nvs-flash`, this will regenerate
367+
and flash the `nvs` partition.
368+
369+
For more information about the format of this file see Espressif's
370+
[documentation for the NVS generator file format](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/storage/nvs_partition_gen.html#csv-file-format).
371+
307372
### Running tests for ESP32
308373
309374
Tests for ESP32 are run on the desktop (or CI) using qemu.

src/platforms/esp32/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
sdkconfig.defaults
77
sdkconfig.old
8+
nvs_partition.csv

src/platforms/esp32/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,7 @@ if (NOT ("${BOOT_LIBS}" STREQUAL "NONE"))
9595
partition_table_get_partition_info(lib_offset "--partition-name boot.avm" "offset")
9696
esptool_py_flash_target_image(flash boot.avm "${lib_offset}" "${BOOT_LIB_PATH}")
9797
endif()
98+
99+
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/nvs_partition.csv)
100+
nvs_create_partition_image(nvs ${CMAKE_CURRENT_SOURCE_DIR}/nvs_partition.csv)
101+
endif()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
key,type,encoding,value
2+
network,namespace,,
3+
ssid,data,string,"NETWORK_NAME"
4+
psk,data,string,"PASSWORD"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SPDX-License-Identifier: Apache-2.0
2+
SPDX-FileCopyrightText: AtomVM Contributors

src/platforms/esp32/tools/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ endif()
5757

5858
set(ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../")
5959

60-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/mkimage.config.in ${CMAKE_BINARY_DIR}/mkimage.config)
60+
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../nvs_partition.csv)
61+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/mkimage_nvs.config.in ${CMAKE_BINARY_DIR}/mkimage.config)
62+
else()
63+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/mkimage.config.in ${CMAKE_BINARY_DIR}/mkimage.config)
64+
endif()
6165

6266
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/flash.sh.in ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/flash.sh @ONLY)
6367
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/mkimage.erl ${CMAKE_BINARY_DIR}/mkimage.erl COPYONLY)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2020-2021 Fred Dushin <[email protected]>
5+
% Copyright 2025 Winford (UncleGrumpy) <[email protected]>
6+
%
7+
% Licensed under the Apache License, Version 2.0 (the "License");
8+
% you may not use this file except in compliance with the License.
9+
% You may obtain a copy of the License at
10+
%
11+
% http://www.apache.org/licenses/LICENSE-2.0
12+
%
13+
% Unless required by applicable law or agreed to in writing, software
14+
% distributed under the License is distributed on an "AS IS" BASIS,
15+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
% See the License for the specific language governing permissions and
17+
% limitations under the License.
18+
%
19+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
20+
%
21+
22+
#{
23+
segments => [
24+
#{
25+
name => "bootloader",
26+
offset => "${BOOTLOADER_OFFSET}",
27+
path => ["${BUILD_DIR}/bootloader/bootloader.bin"]
28+
},
29+
#{
30+
name => "partition-table",
31+
offset => "0x8000",
32+
path => ["${BUILD_DIR}/partition_table/partition-table.bin", "${BUILD_DIR}/partitions.bin"]
33+
},
34+
#{
35+
name => "nvs",
36+
offset => "0x9000",
37+
path => ["${BUILD_DIR}/nvs.bin"]
38+
},
39+
#{
40+
name => "AtomVM Virtual Machine",
41+
offset => "0x10000",
42+
path => ["${BUILD_DIR}/atomvm-esp32.bin", "${ROOT_DIR}/src/platforms/esp32/build/atomvm-esp32.bin"]
43+
},
44+
#{
45+
name => "AtomVM Boot and Core BEAM Library",
46+
offset => "0x1D0000",
47+
path => ["${ROOT_DIR}/build/libs/esp32boot/${BOOT_LIBS}"]
48+
}
49+
]
50+
}.

0 commit comments

Comments
 (0)