Skip to content

Commit 123d3d7

Browse files
committed
feat(esp_ext_part_tables): Add support for BDL
1 parent 6af5d2c commit 123d3d7

File tree

6 files changed

+347
-8
lines changed

6 files changed

+347
-8
lines changed

esp_ext_part_tables/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ set(srcs "src/esp_ext_part_tables.c"
22
"src/esp_mbr.c"
33
"src/esp_mbr_utils.c")
44

5+
set(requires "log" "esp_common")
6+
7+
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "6.0")
8+
list(APPEND requires "esp_blockdev")
9+
endif()
10+
511
idf_component_register(SRCS ${srcs}
612
INCLUDE_DIRS "include"
7-
REQUIRES "log" "esp_common")
13+
REQUIRES ${requires})

esp_ext_part_tables/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.1.1"
1+
version: "0.2.0"
22
description: ESP External Partition Tables
33
url: https://github.com/espressif/idf-extra-components/tree/master/esp_ext_part_tables
44
issues: https://github.com/espressif/idf-extra-components/issues

esp_ext_part_tables/include/esp_ext_part_tables.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
#include <stdbool.h>
1111
#include <stddef.h>
1212
#include "esp_err.h"
13+
#include "esp_idf_version.h"
14+
15+
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
16+
#include "esp_blockdev.h"
17+
#endif // (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
1318

1419
#if __has_include(<bsd/sys/queue.h>)
1520
#include <bsd/sys/queue.h>
@@ -209,6 +214,54 @@ esp_err_t esp_ext_part_list_signature_get(esp_ext_part_list_t *part_list, void *
209214
*/
210215
esp_err_t esp_ext_part_list_signature_set(esp_ext_part_list_t *part_list, const void *signature, esp_ext_part_signature_type_t type);
211216

217+
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
218+
/**
219+
* @brief Read a aprtition table and from a block device handle and parse it.
220+
*
221+
* This function reads the partition table from the specified block device and populates the provided partition list structure.
222+
* The type of partition table to read is specified by the 'type' parameter.
223+
* Additional arguments for parsing can be provided through the 'extra_args' parameter.
224+
*
225+
* @note This function is not thread-safe.
226+
*
227+
* @param[in] handle Block device handle to read from.
228+
* @param[out] part_list Pointer to the partition list structure to populate from the partition table.
229+
* @param[in] type Type of partition table to read (e.g., MBR).
230+
* @param[in] extra_args Pointer to additional arguments for parsing dependent on the partition type (optional, can be NULL).
231+
*
232+
* @return
233+
* - ESP_OK: Partition list was successfully loaded.
234+
* - ESP_ERR_INVALID_ARG: `handle` or `part_list` is NULL.
235+
* - ESP_ERR_NOT_SUPPORTED: Unsupported partition table type.
236+
* - ESP_ERR_NO_MEM: Memory allocation failed.
237+
* - propagated errors from BDL operations or partition table parsing functions.
238+
*/
239+
esp_err_t esp_ext_part_list_read(esp_blockdev_handle_t handle, esp_ext_part_list_t *part_list, esp_ext_part_signature_type_t type, void *extra_args);
240+
241+
/**
242+
* @brief Generate a partition table and write it to a block device handle.
243+
*
244+
* This function writes the provided partition list to the specified block device.
245+
* The type of partition table to write is specified by the 'type' parameter.
246+
* Additional arguments for generation can be provided through the 'extra_args' parameter.
247+
*
248+
* @note This function is not thread-safe.
249+
*
250+
* @param[in] handle Block device handle to write to.
251+
* @param[in] part_list Pointer to the partition list structure generate the partition table from.
252+
* @param[in] type Type of partition table to write (e.g., MBR).
253+
* @param[in] extra_args Pointer to additional arguments for generation dependent on the partition type (optional, can be NULL).
254+
*
255+
* @return
256+
* - ESP_OK: Partition list was successfully written.
257+
* - ESP_ERR_INVALID_ARG: `handle` or `part_list` is NULL.
258+
* - ESP_ERR_NOT_SUPPORTED: Unsupported partition table type.
259+
* - ESP_ERR_NO_MEM: Memory allocation failed.
260+
* - propagated errors from BDL operations or partition table generation functions.
261+
*/
262+
esp_err_t esp_ext_part_list_write(esp_blockdev_handle_t handle, esp_ext_part_list_t *part_list, esp_ext_part_signature_type_t type, void *extra_args);
263+
#endif // (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
264+
212265
#ifdef __cplusplus
213266
}
214267
#endif

esp_ext_part_tables/src/esp_ext_part_tables.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
#include "freertos/FreeRTOS.h"
1010
#include "esp_err.h"
1111
#include "esp_log.h"
12+
#include "esp_idf_version.h"
13+
14+
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
15+
#include "esp_blockdev.h"
16+
#endif // (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
1217

1318
#include "esp_ext_part_tables.h"
19+
#include "esp_mbr.h"
1420

1521
#if __has_include(<bsd/sys/queue.h>)
1622
#include <bsd/sys/queue.h>
@@ -153,3 +159,73 @@ esp_err_t esp_ext_part_list_signature_set(esp_ext_part_list_t *part_list, const
153159
}
154160
return ESP_OK;
155161
}
162+
163+
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
164+
esp_err_t esp_ext_part_list_read(esp_blockdev_handle_t handle, esp_ext_part_list_t *part_list, esp_ext_part_signature_type_t type, void *extra_args)
165+
{
166+
if (handle == NULL || part_list == NULL) {
167+
return ESP_ERR_INVALID_ARG;
168+
}
169+
170+
esp_err_t err = ESP_OK;
171+
uint8_t *buf = NULL;
172+
173+
switch (type) {
174+
case ESP_EXT_PART_LIST_SIGNATURE_MBR:
175+
buf = malloc(MBR_SIZE);
176+
if (buf == NULL) {
177+
return ESP_ERR_NO_MEM;
178+
}
179+
180+
err = handle->ops->read(handle, buf, MBR_SIZE, 0, MBR_SIZE);
181+
if (err != ESP_OK) {
182+
free(buf);
183+
return err;
184+
}
185+
186+
err = esp_mbr_parse(buf, part_list, (esp_mbr_parse_extra_args_t *) extra_args);
187+
free(buf);
188+
break;
189+
190+
default:
191+
err = ESP_ERR_NOT_SUPPORTED; // Unsupported signature type
192+
break;
193+
}
194+
195+
return err;
196+
}
197+
198+
esp_err_t esp_ext_part_list_write(esp_blockdev_handle_t handle, esp_ext_part_list_t *part_list, esp_ext_part_signature_type_t type, void *extra_args)
199+
{
200+
if (handle == NULL || part_list == NULL) {
201+
return ESP_ERR_INVALID_ARG;
202+
}
203+
204+
esp_err_t err = ESP_OK;
205+
uint8_t *buf = NULL;
206+
207+
switch (type) {
208+
case ESP_EXT_PART_LIST_SIGNATURE_MBR:
209+
buf = malloc(MBR_SIZE);
210+
if (buf == NULL) {
211+
return ESP_ERR_NO_MEM;
212+
}
213+
214+
err = esp_mbr_generate((mbr_t *) buf, part_list, (esp_mbr_generate_extra_args_t *) extra_args);
215+
if (err != ESP_OK) {
216+
free(buf);
217+
return err;
218+
}
219+
220+
err = handle->ops->write(handle, buf, 0, MBR_SIZE);
221+
free(buf);
222+
break;
223+
224+
default:
225+
err = ESP_ERR_NOT_SUPPORTED; // Unsupported signature type
226+
break;
227+
}
228+
229+
return err;
230+
}
231+
#endif // (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0))
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
set(priv_requires "unity")
2+
3+
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "6.0")
4+
list(APPEND priv_requires "esp_blockdev")
5+
endif()
6+
17
idf_component_register(SRCS "test_esp_ext_part.c"
28
INCLUDE_DIRS "."
3-
PRIV_REQUIRES "unity"
9+
PRIV_REQUIRES ${priv_requires}
410
WHOLE_ARCHIVE)

0 commit comments

Comments
 (0)