diff --git a/src/lib/parameters/ParameterTest.cpp b/src/lib/parameters/ParameterTest.cpp index b0e04923e91..c3e70d7a8c7 100644 --- a/src/lib/parameters/ParameterTest.cpp +++ b/src/lib/parameters/ParameterTest.cpp @@ -31,12 +31,15 @@ * ****************************************************************************/ +#include #include #include #include #include #include +#include +#include class ParameterTest : public ::testing::Test { @@ -106,3 +109,37 @@ TEST_F(ParameterTest, testUorbSendReceive) // AND: all the bytes should be equal EXPECT_EQ(0, memcmp(&message, &obstacle_distance, sizeof(message))); } + +// A BSON null in an import resets the param (a delta tombstone on flash boards). +TEST_F(ParameterTest, testImportTombstoneResets) +{ + const param_t cp_dist = param_handle(px4::params::CP_DIST); + + float cp = 5.f; + ASSERT_EQ(0, param_set(cp_dist, &cp)); + ASSERT_FALSE(param_value_is_default(cp_dist)); + + bson_encoder_s encoder{}; + ASSERT_EQ(0, bson_encoder_init_buf(&encoder, nullptr, 0)); + ASSERT_EQ(0, bson_encoder_append_int32(&encoder, "RC1_REV", -1)); + ASSERT_EQ(0, bson_encoder_append_null(&encoder, "CP_DIST")); + ASSERT_EQ(0, bson_encoder_fini(&encoder)); + + char path[] = "/tmp/param_tombstone_XXXXXX"; + int fd = mkstemp(path); + ASSERT_GE(fd, 0); + ASSERT_EQ((ssize_t)bson_encoder_buf_size(&encoder), write(fd, bson_encoder_buf_data(&encoder), + bson_encoder_buf_size(&encoder))); + free(bson_encoder_buf_data(&encoder)); + ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)); + + EXPECT_EQ(0, param_import(fd)); + close(fd); + unlink(path); + + EXPECT_TRUE(param_value_is_default(cp_dist)); + + int32_t rev = 0; + ASSERT_EQ(0, param_get(param_handle(px4::params::RC1_REV), &rev)); + EXPECT_EQ(-1, rev); +} diff --git a/src/lib/parameters/flashparams/flashfs.c b/src/lib/parameters/flashparams/flashfs.c index 7ea54fc0586..75fa450a9c8 100644 --- a/src/lib/parameters/flashparams/flashfs.c +++ b/src/lib/parameters/flashparams/flashfs.c @@ -122,6 +122,10 @@ static int last_erased; ****************************************************************************/ const flash_file_token_t parameters_token = { + .n = {'p', 'l', 'o', 'g'}, +}; + +const flash_file_token_t parameters_legacy_token = { .n = {'p', 'a', 'r', 'm'}, }; @@ -220,46 +224,6 @@ static inline int valid_magic(h_magic_t *pm) return *pm == MagicSig; } -/**************************************************************************** - * Name: blank_magic - * - * Description: - * This helper function returns true if the pointer points to a valid - * blank magic signature - * - * Input Parameters: - * pm - A pointer to memory aligned on sizeof(h_magic_t) boundaries - * - * Returned value: - * true if magic is valid - * - * - ****************************************************************************/ - -static inline int blank_magic(h_magic_t *pm) -{ - return *pm == BlankSig; -} -/**************************************************************************** - * Name: erased_entry - * - * Description: - * This helper function returns true if the entry is Erased - * - * Input Parameters: - * fi - A pointer to the current flash entry header - * - * Returned value: - * true if erased - * - * - ****************************************************************************/ - -static inline int erased_entry(flash_entry_header_t *fi) -{ - return (fi->flag & MaskEntry) == ErasedEntry; -} - /**************************************************************************** * Name: blank_entry * @@ -424,217 +388,233 @@ static inline data_size_t entry_crc_length(flash_entry_header_t *fi) return fi->size - offsetof(flash_entry_header_t, size); } +/* The parameter sector can be the last one on the chip, so no header + * field may be read until the whole header is known to lie inside it. */ +static inline bool header_fits(flash_entry_header_t *pf, h_magic_t *pe) +{ + return pf + 1 <= (flash_entry_header_t *)pe; +} + +static bool crc_header_ok(flash_entry_header_t *pf, h_magic_t *pe) +{ + if (!header_fits(pf, pe)) { + return false; + } + + if (pf->size < sizeof(flash_entry_header_t)) { + return false; + } + + const uint8_t *crc_start = entry_crc_start(pf); + data_size_t crc_length = entry_crc_length(pf); + + if (crc_start + crc_length > (uint8_t *)pe) { + return false; + } + + return pf->crc == crc32(crc_start, crc_length); +} + +/* The walkers stop at the last word of a sector and reject a record that + * ends beyond it, so nothing may be written into that word. */ +static inline size_t sector_usable(int s) +{ + return sector_map[s].size - sizeof(h_magic_t); +} + +static bool slot_is_free(flash_entry_header_t *pf, int s, size_t required) +{ + uint8_t *begin = (uint8_t *)sector_map[s].address; + uint8_t *end = begin + sector_usable(s); + + if ((uint8_t *)pf < begin || required == 0 || (uint8_t *)pf + required > end) { + return false; + } + + /* Entries start on a flash row. up_progmem_write only asserts that in + * debug builds, so a slot found by the word-wise scan behind a torn + * record would otherwise be programmed misaligned. */ + if (((uintptr_t)pf & SizeMask) != 0) { + return false; + } + + return blank_entry(pf) && blank_check(pf, required); +} + +static int for_each_valid_entry(flash_file_token_t token, + int (*cb)(flash_entry_header_t *pf, void *arg), + void *arg); + +static int stop_at_first(flash_entry_header_t *pf, void *arg) +{ + *(flash_entry_header_t **)arg = pf; + return -ECANCELED; +} + +/* Oldest valid entry for token, or NULL. */ +static flash_entry_header_t *find_entry(flash_file_token_t token) +{ + flash_entry_header_t *pf = NULL; + for_each_valid_entry(token, stop_at_first, &pf); + return pf; +} + /**************************************************************************** - * Name: find_entry + * Name: find_append * * Description: - * This helper function locates an "file" from the the file token - * - * Input Parameters: - * token - A flash file token, the pseudo file name - * - * Returned value: - * On Success a pointer to flash entry header or NULL on failure - * + * Return the address of a blank run that sits after every CRC-valid + * header in sector-map order. First-fit holes before later live data are + * not used; those are fragmentation and belong to compact. * ****************************************************************************/ -static flash_entry_header_t *find_entry(flash_file_token_t token) +static flash_entry_header_t *find_append(size_t required) { + if (sector_map == NULL || required == 0) { + return NULL; + } + + flash_entry_header_t *last = NULL; + int last_s = -1; + + for (int s = 0; sector_map[s].address; s++) { + h_magic_t *pmagic = (h_magic_t *) sector_map[s].address; + h_magic_t *pe = pmagic + (sector_map[s].size / sizeof(h_magic_t)) - 1; + + while (pmagic < pe) { + if (!valid_magic(pmagic)) { + pmagic++; + continue; + } + + flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; + + if (crc_header_ok(pf, pe)) { + last = pf; + last_s = s; + pmagic = (h_magic_t *) next_entry(pf); + pf = (flash_entry_header_t *) pmagic; + + /* Same stop rule as for_each_valid_entry: a record behind a + * blank header is invisible to replay, so it must not anchor + * the append point either. */ + if (pmagic >= pe || !header_fits(pf, pe) || blank_entry(pf)) { + break; + } + + } else { + pmagic++; + } + } + } + + const int start_s = (last == NULL) ? 0 : last_s; + h_magic_t *pmagic = (last == NULL) + ? (h_magic_t *) sector_map[0].address + : (h_magic_t *) next_entry(last); + + for (int s = start_s; sector_map[s].address; s++) { + if (s != start_s) { + pmagic = (h_magic_t *) sector_map[s].address; + } + + h_magic_t *pe = (h_magic_t *) sector_map[s].address + + (sector_map[s].size / sizeof(h_magic_t)) - 1; + + if ((uint8_t *)pmagic < (uint8_t *)sector_map[s].address + || (uint8_t *)pmagic >= (uint8_t *)sector_map[s].address + sector_map[s].size) { + if (s == start_s) { + continue; + } + + pmagic = (h_magic_t *) sector_map[s].address; + } + + while (pmagic < pe) { + flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; + + if (valid_magic(pmagic) && crc_header_ok(pf, pe)) { + pmagic = (h_magic_t *) next_entry(pf); + continue; + } + + if (slot_is_free(pf, s, required)) { + return pf; + } + + pmagic++; + } + } + + return NULL; +} + +/**************************************************************************** + * Name: for_each_valid_entry + * + * Description: + * Visit every CRC-valid, not-erased entry matching token, oldest first. + * cb returns 0 to continue, <0 to abort with that errno. + * A torn or oversize header advances one magic word; it must not skip + * the rest of the sector (later valid records would be invisible). + * + ****************************************************************************/ + +static int for_each_valid_entry(flash_file_token_t token, + int (*cb)(flash_entry_header_t *pf, void *arg), + void *arg) +{ + int n = 0; + + if (sector_map == NULL) { + return -ENXIO; + } + for (int s = 0; sector_map[s].address; s++) { h_magic_t *pmagic = (h_magic_t *) sector_map[s].address; h_magic_t *pe = pmagic + (sector_map[s].size / sizeof(h_magic_t)) - 1; - /* Hunt for Magic Signature */ cont: while (pmagic < pe && !valid_magic(pmagic)) { pmagic++; } - /* Did we reach the end - * if so try the next sector */ - if (pmagic >= pe) { continue; } - /* Found a magic So assume it is a file header */ - flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; - /* Ensure that the header is fully inside the current sector */ - - if (pf + 1 > (flash_entry_header_t *)pe) { continue; } - - const uint8_t *crc_start = entry_crc_start(pf); - data_size_t crc_length = entry_crc_length(pf); - - if (crc_start + crc_length > (uint8_t *)pe) { continue; } - - if (pf->crc == crc32(crc_start, crc_length)) { - - /* Good CRC is it the one we are looking for ?*/ + if (crc_header_ok(pf, pe)) { if (valid_entry(pf) && pf->file_token.t == token.t) { + if (cb != NULL) { + int rv = cb(pf, arg); - return pf; - - } else { - - /* Not the one we wanted but we can trust the size */ - - pf = next_entry(pf); - pmagic = (h_magic_t *) pf; - - /* If the next one is erased */ - - if (pmagic >= pe || blank_entry(pf)) { - continue; + if (rv < 0) { + return rv; + } } + + n++; + } + + pf = next_entry(pf); + pmagic = (h_magic_t *) pf; + + if (pmagic >= pe || !header_fits(pf, pe) || blank_entry(pf)) { + continue; } } else { - - /* invalid CRC so keep looking */ - pmagic++; } goto cont; } - return NULL; -} - -/**************************************************************************** - * Name: find_free - * - * Description: - * This helper function locates free space for the number of bytes required - * - * Input Parameters: - * required - Number of bytes required - * - * Returned value: - * On Success a pointer to flash entry header or NULL on failure - * - * - ****************************************************************************/ - -static flash_entry_header_t *find_free(data_size_t required) -{ - for (int s = 0; sector_map[s].address; s++) { - - h_magic_t *pmagic = (h_magic_t *) sector_map[s].address; - h_magic_t *pe = pmagic + (sector_map[s].size / sizeof(h_magic_t)) - 1; - - /* Hunt for Magic Signature */ - - do { - - if (valid_magic(pmagic)) { - - flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; - - /* Ensure that the header is fully inside the current sector */ - - if (pf + 1 > (flash_entry_header_t *)pe) { break; } - - /* Test the CRC */ - - if (pf->crc == crc32(entry_crc_start(pf), entry_crc_length(pf))) { - - /* Valid Magic and CRC look for the next record*/ - - pmagic = ((uint32_t *) next_entry(pf)); - - } else { - - pmagic++; - } - } - - if (pmagic + (required / sizeof(h_magic_t)) <= pe && blank_magic(pmagic)) { - - flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; - - if (blank_entry(pf) && blank_check(pf, required)) { - return pf; - } - - } - } while (++pmagic < pe); - } - - return NULL; -} - -/**************************************************************************** - * Name: get_next_sector_descriptor - * - * Description: - * Given a pointer to sector_descriptor_t, this helper function - * returns a pointer to the next sector_descriptor_t - * - * Input Parameters: - * current - A pointer to the current sector_descriptor_t - * - * Returned value: - * On Success A pointer to the next sector_descriptor_t, - * otherwise NULL - * - * - ****************************************************************************/ - -static sector_descriptor_t *get_next_sector_descriptor(sector_descriptor_t * - current) -{ - for (int s = 0; sector_map[s].address; s++) { - if (current == §or_map[s]) { - if (sector_map[s + 1].address) { - s++; - - } else { - s = 0; - } - - return §or_map[s]; - } - } - - return NULL; -} - -/**************************************************************************** - * Name: get_sector_info - * - * Description: - * Given a pointer to a flash entry header returns the sector descriptor - * for the file is located in - * - * Input Parameters: - * current - A pointer to the current flash entry header - * - * Returned value: - * On Success A pointer to the next sector_descriptor_t, - * otherwise NULL - * - * - ****************************************************************************/ - -static sector_descriptor_t *get_sector_info(flash_entry_header_t *current) -{ - for (int s = 0; sector_map[s].address != 0; s++) { - uint8_t *pb = (uint8_t *) sector_map[s].address; - uint8_t *pe = pb + sector_map[s].size - 1; - uint8_t *pc = (uint8_t *) current; - - if (pc >= pb && pc <= pe) { - return §or_map[s]; - } - } - - return 0; + return n; } /**************************************************************************** @@ -680,65 +660,6 @@ static int erase_sector(sector_descriptor_t *sm, flash_entry_header_t *pf) return rv; } -/**************************************************************************** - * Name: erase_entry - * - * Description: - * Given a pointer to a flash entry header erases the entry - * - * Input Parameters: - * pf - A pointer to the current flash entry header - * - * - * Returned value: - * >0 On Success or a negative errno - * - * - ****************************************************************************/ - -static int erase_entry(flash_entry_header_t *pf) -{ - h_flag_t data = ErasedEntry; - size_t size = sizeof(h_flag_t); -#if defined(BOARD_USE_EXTERNAL_FLASH) - int rv = up_progmem_ext_write((size_t) &pf->flag, &data, size); -#else - int rv = up_progmem_write((size_t) &pf->flag, &data, size); -#endif - return rv; -} - -/**************************************************************************** - * Name: check_free_space_in_sector - * - * Description: - * Given a pointer to a flash entry header and a new size - * - * Input Parameters: -* pf - A pointer to the current flash entry header - * new_size - The total number of bytes to be written - * - * Returned value: - * 0 if there is enough space left to write new size - * If not it returns the flash_file_sector_t * that needs to be erased. - * - ****************************************************************************/ - -static sector_descriptor_t *check_free_space_in_sector(flash_entry_header_t - *pf, size_t new_size) -{ - sector_descriptor_t *sm = get_sector_info(pf); - uint8_t *psector_first = (uint8_t *) sm->address; - uint8_t *psector_last = psector_first + sm->size - 1; - uint8_t *pnext_end = (uint8_t *)(valid_magic((h_magic_t *)pf) ? next_entry(pf) : pf) + new_size; - - if (pnext_end >= psector_first && pnext_end <= psector_last) { - sm = 0; - } - - return sm; -} - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -815,8 +736,7 @@ int parameter_flashfs_blank(void) * Name: parameter_flashfs_write * * Description: - * This function writes user data from the buffer allocated with a previous call - * to parameter_flashfs_alloc. flash starting at the given address + * Append a new entry. Previous valid entries are left intact. * * Input Parameters: * token - File Token File to read @@ -846,82 +766,15 @@ parameter_flashfs_write(flash_file_token_t token, uint8_t *buffer, size_t buf_si size_t size_adjust = ((total_size + alignment) & ~alignment) - total_size; total_size += size_adjust; - /* Is this and existing entry */ + if (total_size > UINT16_MAX) { + return -EFBIG; + } - flash_entry_header_t *pf = find_entry(token); - - if (!pf) { - - /* No Entry exists for this token so find a place for it */ - - pf = find_free(total_size); - - /* No Space */ - - if (pf == 0) { - return -ENOSPC; - } - - } else { - - /* Do we have space after the entry in the sector for the update */ - - sector_descriptor_t *current_sector = check_free_space_in_sector(pf, - total_size); - - - if (current_sector == 0) { - - /* Mark the last entry erased */ - - /* todo:consider a 2 stage erase or write before erase and do a fs check - * at start up - */ - - rv = erase_entry(pf); - - if (rv < 0) { - return rv; - } - - /* We had space and marked the last entry erased so use the Next Free */ - - pf = next_entry(pf); - - } else { - - /* - * We did not have space in the current sector so select the next sector - */ - - current_sector = get_next_sector_descriptor(current_sector); - - /* Will the data fit */ - - if (current_sector->size < total_size) { - return -ENOSPC; - } - - /* Mark the last entry erased */ - - /* todo:consider a 2 stage erase or write before erase and do a fs check - * at start up - */ - - rv = erase_entry(pf); - - if (rv < 0) { - return rv; - } - - pf = (flash_entry_header_t *) current_sector->address; - - if (!blank_check(pf, total_size)) { - rv = erase_sector(current_sector, pf); - } - - } + /* Append after the last CRC-valid record. Compaction is the caller's job. */ + flash_entry_header_t *pf = find_append(total_size); + if (pf == 0) { + return -ENOSPC; } flash_entry_header_t *pn = (flash_entry_header_t *)(buffer - sizeof(flash_entry_header_t)); @@ -950,6 +803,104 @@ parameter_flashfs_write(flash_file_token_t token, uint8_t *buffer, size_t buf_si return rv; } +/**************************************************************************** + * Name: parameter_flashfs_walk + ****************************************************************************/ + +struct walk_pack { + parameter_flashfs_walk_cb cb; + void *arg; +}; + +static int walk_one(flash_entry_header_t *pf, void *arg) +{ + struct walk_pack *w = (struct walk_pack *)arg; + return w->cb(entry_data(pf), entry_data_length(pf), w->arg); +} + +int parameter_flashfs_walk(flash_file_token_t token, parameter_flashfs_walk_cb cb, void *arg) +{ + struct walk_pack w = { cb, arg }; + return for_each_valid_entry(token, walk_one, &w); +} + +/**************************************************************************** + * Name: parameter_flashfs_needs_compact + ****************************************************************************/ + +size_t parameter_flashfs_max_payload(void) +{ + if (sector_map == NULL) { + return 0; + } + + size_t max_total = 0; + + for (int s = 0; sector_map[s].address; s++) { + if (sector_usable(s) > max_total) { + max_total = sector_usable(s); + } + } + + /* h_size_t holds the record size */ + if (max_total > UINT16_MAX) { + max_total = UINT16_MAX; + } + + max_total &= ~(size_t)SizeMask; + + if (max_total <= sizeof(flash_entry_header_t)) { + return 0; + } + + return max_total - sizeof(flash_entry_header_t); +} + +int parameter_flashfs_needs_compact(flash_file_token_t token, size_t snapshot_size) +{ + if (sector_map == NULL) { + return -ENXIO; + } + + flash_entry_header_t *first = find_entry(token); + + if (first == NULL) { + return 0; + } + + /* Dead records ahead of the live data (a torn write, or the erased + * prefix the single-snapshot writer left) only go away with an erase. */ + if ((uintptr_t)first != (uintptr_t)sector_map[0].address) { + return 1; + } + + /* Keep room after the log for a burst the size of the snapshot + * (reset-all tombstones, airframe PARAM_SET), floored so a UUID append + * does not force a boot erase. */ + const size_t align = sizeof(h_magic_t) - 1; + const size_t snapshot_total = (snapshot_size + sizeof(flash_entry_header_t) + align) & ~align; + const size_t headroom = snapshot_total > 8192 ? snapshot_total : 8192; + + if (find_append(headroom) != NULL) { + return 0; + } + + /* Compaction leaves the snapshot at the start of sector 0 and every + * other sector blank. Erase only if that layout has the room this one + * lacks, or every boot would pay the erase for nothing. */ + if (sector_usable(0) >= snapshot_total + headroom) { + return 1; + } + + for (int s = 1; sector_map[s].address; s++) { + if (sector_usable(s) >= headroom) { + return 1; + } + } + + return 0; +} + /**************************************************************************** * Name: parameter_flashfs_alloc * @@ -982,7 +933,8 @@ int parameter_flashfs_alloc(flash_file_token_t token, uint8_t **buffer, size_t * if (!working_buffer_static) { - working_buffer_size = *buf_size + sizeof(flash_entry_header_t); + /* parameter_flashfs_write pads the entry to a word in place */ + working_buffer_size = *buf_size + sizeof(flash_entry_header_t) + SizeMask; working_buffer = malloc(working_buffer_size); } @@ -997,7 +949,7 @@ int parameter_flashfs_alloc(flash_file_token_t token, uint8_t **buffer, size_t * /* We have a buffer reserve space and init it */ *buffer = &working_buffer[sizeof(flash_entry_header_t)]; - *buf_size = working_buffer_size - sizeof(flash_entry_header_t); + *buf_size = working_buffer_size - sizeof(flash_entry_header_t) - SizeMask; memset(working_buffer, 0xff, working_buffer_size); rv = 0; @@ -1086,31 +1038,9 @@ int parameter_flashfs_init(sector_descriptor_t *fconfig, uint8_t *buffer, uint16 /* Sanity check */ - flash_entry_header_t *pf = find_entry(parameters_token); - - /* No parameters */ - - if (pf == NULL) { - size_t total_size = size + sizeof(flash_entry_header_t); - size_t alignment = sizeof(h_magic_t) - 1; - size_t size_adjust = ((total_size + alignment) & ~alignment) - total_size; - total_size += size_adjust; - - /* Do we have free space ?*/ - - if (find_free(total_size) == NULL) { - - /* No paramates and no free space => neeed erase */ - - rv = parameter_flashfs_erase(); - - /* A positive return value means flash space has been erased successfully */ - - if (rv > 0) { - rv = 0; - } - } - } + /* A non-blank store with no valid entry is torn or foreign; import + * reports -EILSEQ. Erasing here would hide a successful retry after + * a torn write. */ return rv; } @@ -1146,6 +1076,7 @@ __EXPORT void test(void) int rv = 0; for (int a = 0; a <= 4; a++) { + parameter_flashfs_erase(); rv = parameter_flashfs_alloc(parameters_token, &fbuffer, &buf_size); memcpy(fbuffer, test_buf, a); buf_size = a; @@ -1162,6 +1093,7 @@ __EXPORT void test(void) int block = 2048; for (int a = 0; a <= 8; a++) { + parameter_flashfs_erase(); rv = parameter_flashfs_alloc(parameters_token, &fbuffer, &buf_size); memcpy(fbuffer, test_buf, block); buf_size = block; diff --git a/src/lib/parameters/flashparams/flashfs.h b/src/lib/parameters/flashparams/flashfs.h index ed181cc04da..4d3306cf15c 100644 --- a/src/lib/parameters/flashparams/flashfs.h +++ b/src/lib/parameters/flashparams/flashfs.h @@ -48,6 +48,7 @@ #include #include +#include @@ -80,12 +81,14 @@ typedef struct flash_file_token_t { } flash_file_token_t; /* - * Define the parameter "file name" Currently there is only - * and it is hard coded. If more are added the - * parameter_flashfs_write would need to support a backing buffer - * for when a sector is erased. + * Parameter "file names". parameters_token marks records of the append log + * (a snapshot followed by deltas, replayed in order). parameters_legacy_token + * is the single-snapshot record written by firmware before the log format; + * that firmware ignores parameters_token records, so it boots on defaults + * from a log-format store rather than a partial set. */ __EXPORT extern const flash_file_token_t parameters_token; +__EXPORT extern const flash_file_token_t parameters_legacy_token; /* Define the elements of the array passed to the * parameter_flashfs_init function @@ -156,6 +159,22 @@ __EXPORT int parameter_flashfs_init(sector_descriptor_t *fconfig, uint8_t *buffe __EXPORT int parameter_flashfs_read(flash_file_token_t ft, uint8_t **buffer, size_t *buf_size); +/**************************************************************************** + * Name: parameter_flashfs_walk + * + * Description: + * Call cb for every valid entry matching token, in the order they were + * written. cb returns 0 to continue, or a negative errno to abort. + * + * Returned value: + * Number of entries visited, or a negative errno (including from cb). + * + ****************************************************************************/ + +typedef int (*parameter_flashfs_walk_cb)(uint8_t *buffer, size_t buf_size, void *arg); + +__EXPORT int parameter_flashfs_walk(flash_file_token_t token, parameter_flashfs_walk_cb cb, void *arg); + /**************************************************************************** * Name: parameter_flashfs_blank * @@ -178,8 +197,10 @@ __EXPORT int parameter_flashfs_blank(void); * Name: parameter_flashfs_write * * Description: - * This function writes user data from the buffer allocated with a previous call - * to parameter_flashfs_alloc. flash starting at the given address + * Append a new entry after the last CRC-valid record. Previous valid + * entries are left intact so a boot replay can apply the whole log; the + * caller erases and rewrites a snapshot when a burst of deltas would no + * longer fit. Returns -ENOSPC if the new entry does not fit after the log. * * Input Parameters: * token - File Token File to read @@ -212,21 +233,33 @@ __EXPORT int parameter_flashfs_write(flash_file_token_t ft, uint8_t *buffer, siz __EXPORT int parameter_flashfs_erase(void); /**************************************************************************** - * Name: parameter_flashfs_compact_if_full + * Name: parameter_flashfs_needs_compact * * Description: - * If the sector lacks room for another entry the size of the stored one, - * rewrite it through the normal wrap path now (sector erase + rewrite). - * Called at boot so the erase, which stalls every read of its flash bank - * while it runs, cannot land on a save made once the vehicle is armed. + * True when the log for token should be rewritten as one snapshot of + * snapshot_size payload bytes: the live records are not a packed prefix + * of sector 0, or the tail after the last CRC-valid record cannot hold a + * burst the size of that snapshot (floored at 8 KB so a UUID append does + * not force a boot erase) and a compacted layout would. The erase stalls + * every read of that flash bank, so callers run it at boot before sensors + * and DShot start. * * Returned value: - * 1 if a compaction was performed, 0 if there was enough space or nothing - * is stored, or a negative errno. + * 1 if compact is needed, 0 if not, or a negative errno. * ****************************************************************************/ -__EXPORT int parameter_flashfs_compact_if_full(void); +__EXPORT int parameter_flashfs_needs_compact(flash_file_token_t token, size_t snapshot_size); + +/**************************************************************************** + * Name: parameter_flashfs_max_payload + * + * Description: + * Largest user payload that fits in one mapped sector, or 0. + * + ****************************************************************************/ + +__EXPORT size_t parameter_flashfs_max_payload(void); /**************************************************************************** * Name: parameter_flashfs_alloc diff --git a/src/lib/parameters/flashparams/flashfs32.c b/src/lib/parameters/flashparams/flashfs32.c index 459f0ac950b..8e5ab33fec6 100644 --- a/src/lib/parameters/flashparams/flashfs32.c +++ b/src/lib/parameters/flashparams/flashfs32.c @@ -66,8 +66,6 @@ * Pre-processor Definitions ****************************************************************************/ -#define FLASH_FLAG_ROW_SIZE 16 ///< size in h_flag_t units of a flash row - /**************************************************************************** * Private Types ****************************************************************************/ @@ -121,7 +119,7 @@ typedef begin_packed_struct struct flash_entry_header_t { * Private Data ****************************************************************************/ static uint8_t *working_buffer; -static uint16_t working_buffer_size; +static size_t working_buffer_size; static bool working_buffer_static; static sector_descriptor_t *sector_map; @@ -130,6 +128,10 @@ static sector_descriptor_t *sector_map; ****************************************************************************/ const flash_file_token_t parameters_token = { + .n = {'p', 'l', 'o', 'g'}, +}; + +const flash_file_token_t parameters_legacy_token = { .n = {'p', 'a', 'r', 'm'}, }; @@ -228,47 +230,6 @@ static inline int valid_magic(h_magic_t *pm) return *pm == MagicSig; } -/**************************************************************************** - * Name: blank_magic - * - * Description: - * This helper function returns true if the pointer points to a valid - * blank magic signature - * - * Input Parameters: - * pm - A pointer to memory aligned on sizeof(h_magic_t) boundaries - * - * Returned value: - * true if magic is valid - * - * - ****************************************************************************/ - -static inline int blank_magic(h_magic_t *pm) -{ - return *pm == BlankSig; -} - -/**************************************************************************** - * Name: erased_entry - * - * Description: - * This helper function returns true if the entry is Erased - * - * Input Parameters: - * fi - A pointer to the current flash entry header - * - * Returned value: - * true if erased - * - * - ****************************************************************************/ - -static inline int erased_entry(flash_entry_header_t *fi) -{ - return (fi->flag_erased & MaskEntry) == ErasedEntry; -} - /**************************************************************************** * Name: blank_entry * @@ -433,217 +394,233 @@ static inline data_size_t entry_crc_length(flash_entry_header_t *fi) return fi->size - offsetof(flash_entry_header_t, size); } +/* The parameter sector can be the last one on the chip, so no header + * field may be read until the whole header is known to lie inside it. */ +static inline bool header_fits(flash_entry_header_t *pf, h_magic_t *pe) +{ + return pf + 1 <= (flash_entry_header_t *)pe; +} + +static bool crc_header_ok(flash_entry_header_t *pf, h_magic_t *pe) +{ + if (!header_fits(pf, pe)) { + return false; + } + + if (pf->size < sizeof(flash_entry_header_t)) { + return false; + } + + const uint8_t *crc_start = entry_crc_start(pf); + data_size_t crc_length = entry_crc_length(pf); + + if (crc_start + crc_length > (uint8_t *)pe) { + return false; + } + + return pf->crc == crc32(crc_start, crc_length); +} + +/* The walkers stop at the last word of a sector and reject a record that + * ends beyond it, so nothing may be written into that word. */ +static inline size_t sector_usable(int s) +{ + return sector_map[s].size - sizeof(h_magic_t); +} + +static bool slot_is_free(flash_entry_header_t *pf, int s, size_t required) +{ + uint8_t *begin = (uint8_t *)sector_map[s].address; + uint8_t *end = begin + sector_usable(s); + + if ((uint8_t *)pf < begin || required == 0 || (uint8_t *)pf + required > end) { + return false; + } + + /* Entries start on a flash row. up_progmem_write only asserts that in + * debug builds, so a slot found by the word-wise scan behind a torn + * record would otherwise be programmed misaligned. */ + if (((uintptr_t)pf & SizeMask) != 0) { + return false; + } + + return blank_entry(pf) && blank_check(pf, required); +} + +static int for_each_valid_entry(flash_file_token_t token, + int (*cb)(flash_entry_header_t *pf, void *arg), + void *arg); + +static int stop_at_first(flash_entry_header_t *pf, void *arg) +{ + *(flash_entry_header_t **)arg = pf; + return -ECANCELED; +} + +/* Oldest valid entry for token, or NULL. */ +static flash_entry_header_t *find_entry(flash_file_token_t token) +{ + flash_entry_header_t *pf = NULL; + for_each_valid_entry(token, stop_at_first, &pf); + return pf; +} + /**************************************************************************** - * Name: find_entry + * Name: find_append * * Description: - * This helper function locates an "file" from the the file token - * - * Input Parameters: - * token - A flash file token, the pseudo file name - * - * Returned value: - * On Success a pointer to flash entry header or NULL on failure - * + * Return the address of a blank run that sits after every CRC-valid + * header in sector-map order. First-fit holes before later live data are + * not used; those are fragmentation and belong to compact. * ****************************************************************************/ -static flash_entry_header_t *find_entry(flash_file_token_t token) +static flash_entry_header_t *find_append(size_t required) { + if (sector_map == NULL || required == 0) { + return NULL; + } + + flash_entry_header_t *last = NULL; + int last_s = -1; + + for (int s = 0; sector_map[s].address; s++) { + h_magic_t *pmagic = (h_magic_t *) sector_map[s].address; + h_magic_t *pe = pmagic + (sector_map[s].size / sizeof(h_magic_t)) - 1; + + while (pmagic < pe) { + if (!valid_magic(pmagic)) { + pmagic++; + continue; + } + + flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; + + if (crc_header_ok(pf, pe)) { + last = pf; + last_s = s; + pmagic = (h_magic_t *) next_entry(pf); + pf = (flash_entry_header_t *) pmagic; + + /* Same stop rule as for_each_valid_entry: a record behind a + * blank header is invisible to replay, so it must not anchor + * the append point either. */ + if (pmagic >= pe || !header_fits(pf, pe) || blank_entry(pf)) { + break; + } + + } else { + pmagic++; + } + } + } + + const int start_s = (last == NULL) ? 0 : last_s; + h_magic_t *pmagic = (last == NULL) + ? (h_magic_t *) sector_map[0].address + : (h_magic_t *) next_entry(last); + + for (int s = start_s; sector_map[s].address; s++) { + if (s != start_s) { + pmagic = (h_magic_t *) sector_map[s].address; + } + + h_magic_t *pe = (h_magic_t *) sector_map[s].address + + (sector_map[s].size / sizeof(h_magic_t)) - 1; + + if ((uint8_t *)pmagic < (uint8_t *)sector_map[s].address + || (uint8_t *)pmagic >= (uint8_t *)sector_map[s].address + sector_map[s].size) { + if (s == start_s) { + continue; + } + + pmagic = (h_magic_t *) sector_map[s].address; + } + + while (pmagic < pe) { + flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; + + if (valid_magic(pmagic) && crc_header_ok(pf, pe)) { + pmagic = (h_magic_t *) next_entry(pf); + continue; + } + + if (slot_is_free(pf, s, required)) { + return pf; + } + + pmagic++; + } + } + + return NULL; +} + +/**************************************************************************** + * Name: for_each_valid_entry + * + * Description: + * Visit every CRC-valid, not-erased entry matching token, oldest first. + * cb returns 0 to continue, <0 to abort with that errno. + * A torn or oversize header advances one magic word; it must not skip + * the rest of the sector (later valid records would be invisible). + * + ****************************************************************************/ + +static int for_each_valid_entry(flash_file_token_t token, + int (*cb)(flash_entry_header_t *pf, void *arg), + void *arg) +{ + int n = 0; + + if (sector_map == NULL) { + return -ENXIO; + } + for (int s = 0; sector_map[s].address; s++) { h_magic_t *pmagic = (h_magic_t *) sector_map[s].address; h_magic_t *pe = pmagic + (sector_map[s].size / sizeof(h_magic_t)) - 1; - /* Hunt for Magic Signature */ cont: while (pmagic < pe && !valid_magic(pmagic)) { pmagic++; } - /* Did we reach the end - * if so try the next sector */ - if (pmagic >= pe) { continue; } - /* Found a magic So assume it is a file header */ - flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; - /* Ensure that the header is fully inside the current sector */ - - if (pf + 1 > (flash_entry_header_t *)pe) { continue; } - - const uint8_t *crc_start = entry_crc_start(pf); - data_size_t crc_length = entry_crc_length(pf); - - if (crc_start + crc_length > (uint8_t *)pe) { continue; } - - if (pf->crc == crc32(crc_start, crc_length)) { - - /* Good CRC is it the one we are looking for ?*/ + if (crc_header_ok(pf, pe)) { if (valid_entry(pf) && pf->file_token.t == token.t) { + if (cb != NULL) { + int rv = cb(pf, arg); - return pf; - - } else { - - /* Not the one we wanted but we can trust the size */ - - pf = next_entry(pf); - pmagic = (h_magic_t *) pf; - - /* If the next one is erased */ - - if (pmagic >= pe || blank_entry(pf)) { - continue; + if (rv < 0) { + return rv; + } } + + n++; + } + + pf = next_entry(pf); + pmagic = (h_magic_t *) pf; + + if (pmagic >= pe || !header_fits(pf, pe) || blank_entry(pf)) { + continue; } } else { - - /* invalid CRC so keep looking */ - pmagic++; } goto cont; } - return NULL; -} - -/**************************************************************************** - * Name: find_free - * - * Description: - * This helper function locates free space for the number of bytes required - * - * Input Parameters: - * required - Number of bytes required - * - * Returned value: - * On Success a pointer to flash entry header or NULL on failure - * - * - ****************************************************************************/ - -static flash_entry_header_t *find_free(data_size_t required) -{ - for (int s = 0; sector_map[s].address; s++) { - - h_magic_t *pmagic = (h_magic_t *) sector_map[s].address; - h_magic_t *pe = pmagic + (sector_map[s].size / sizeof(h_magic_t)) - 1; - - /* Hunt for Magic Signature */ - - do { - - if (valid_magic(pmagic)) { - - flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; - - /* Ensure that the header is fully inside the current sector */ - - if (pf + 1 > (flash_entry_header_t *)pe) { break; } - - /* Test the CRC */ - - if (pf->crc == crc32(entry_crc_start(pf), entry_crc_length(pf))) { - - /* Valid Magic and CRC look for the next record*/ - - pmagic = ((uint32_t *) next_entry(pf)); - - } else { - - pmagic++; - } - } - - if (pmagic + (required / sizeof(h_magic_t)) <= pe && blank_magic(pmagic)) { - - flash_entry_header_t *pf = (flash_entry_header_t *) pmagic; - - if (blank_entry(pf) && blank_check(pf, required)) { - return pf; - } - - } - } while (++pmagic < pe); - } - - return NULL; -} - -/**************************************************************************** - * Name: get_next_sector_descriptor - * - * Description: - * Given a pointer to sector_descriptor_t, this helper function - * returns a pointer to the next sector_descriptor_t - * - * Input Parameters: - * current - A pointer to the current sector_descriptor_t - * - * Returned value: - * On Success A pointer to the next sector_descriptor_t, - * otherwise NULL - * - * - ****************************************************************************/ - -static sector_descriptor_t *get_next_sector_descriptor(sector_descriptor_t * - current) -{ - for (int s = 0; sector_map[s].address; s++) { - if (current == §or_map[s]) { - if (sector_map[s + 1].address) { - s++; - - } else { - s = 0; - } - - return §or_map[s]; - } - } - - return NULL; -} - -/**************************************************************************** - * Name: get_sector_info - * - * Description: - * Given a pointer to a flash entry header returns the sector descriptor - * for the file is located in - * - * Input Parameters: - * current - A pointer to the current flash entry header - * - * Returned value: - * On Success A pointer to the next sector_descriptor_t, - * otherwise NULL - * - * - ****************************************************************************/ - -static sector_descriptor_t *get_sector_info(flash_entry_header_t *current) -{ - for (int s = 0; sector_map[s].address != 0; s++) { - uint8_t *pb = (uint8_t *) sector_map[s].address; - uint8_t *pe = pb + sector_map[s].size - 1; - uint8_t *pc = (uint8_t *) current; - - if (pc >= pb && pc <= pe) { - return §or_map[s]; - } - } - - return 0; + return n; } /**************************************************************************** @@ -707,63 +684,6 @@ static int erase_sector(sector_descriptor_t *sm, flash_entry_header_t *pf) return rv; } -/**************************************************************************** - * Name: erase_entry - * - * Description: - * Given a pointer to a flash entry header erases the entry - * - * Input Parameters: - * pf - A pointer to the current flash entry header - * - * - * Returned value: - * >0 On Success or a negative errno - * - * - ****************************************************************************/ - -static int erase_entry(flash_entry_header_t *pf) -{ - /* Need to write entire 32-byte line */ - h_flag_t data[FLASH_FLAG_ROW_SIZE] = {0xffff}; - data[0] = ErasedEntry; - size_t size = FLASH_FLAG_ROW_SIZE * sizeof(h_flag_t); - int rv = up_progmem_write((size_t) &pf->flag_erased, data, size); - return rv; -} - -/**************************************************************************** - * Name: check_free_space_in_sector - * - * Description: - * Given a pointer to a flash entry header and a new size - * - * Input Parameters: - * pf - A pointer to the current flash entry header - * new_size - The total number of bytes to be written - * - * Returned value: - * 0 if there is enough space left to write new size - * If not it returns the flash_file_sector_t * that needs to be erased. - * - ****************************************************************************/ - -static sector_descriptor_t *check_free_space_in_sector(flash_entry_header_t - *pf, size_t new_size) -{ - sector_descriptor_t *sm = get_sector_info(pf); - uint8_t *psector_first = (uint8_t *) sm->address; - uint8_t *psector_last = psector_first + sm->size - 1; - uint8_t *pnext_end = (uint8_t *)(valid_magic((h_magic_t *)pf) ? next_entry(pf) : pf) + new_size; - - if (pnext_end >= psector_first && pnext_end <= psector_last) { - sm = 0; - } - - return sm; -} - /**************************************************************************** * Name: write_flash_entry * @@ -886,8 +806,7 @@ int parameter_flashfs_blank(void) * Name: parameter_flashfs_write * * Description: - * This function writes user data from the buffer allocated with a previous call - * to parameter_flashfs_alloc. flash starting at the given address + * Append a new entry. Previous valid entries are left intact. * * Input Parameters: * token - File Token File to read @@ -916,80 +835,15 @@ parameter_flashfs_write(flash_file_token_t token, uint8_t *buffer, size_t buf_si size_t size_adjust = ((total_size + alignment) & ~alignment) - total_size; total_size += size_adjust; - /* Is this and existing entry */ + if (total_size > UINT16_MAX) { + return -EFBIG; + } - flash_entry_header_t *pf = find_entry(token); - - if (!pf) { - - /* No Entry exists for this token so find a place for it */ - pf = find_free(total_size); - - /* No Space */ - - if (pf == 0) { - return -ENOSPC; - } - - } else { - - /* Do we have space after the entry in the sector for the update */ - - sector_descriptor_t *current_sector = check_free_space_in_sector(pf, - total_size); - - - if (current_sector == 0) { - - /* Mark the last entry erased */ - - /* todo:consider a 2 stage erase or write before erase and do a fs check - * at start up - */ - - rv = erase_entry(pf); - - if (rv < 0) { - return rv; - } - - /* We had space and marked the last entry erased so use the Next Free */ - - pf = next_entry(pf); - - } else { - - /* - * We did not have space in the current sector so select the next sector - */ - - current_sector = get_next_sector_descriptor(current_sector); - - /* Will the data fit */ - - if (current_sector->size < total_size) { - return -ENOSPC; - } - - /* Mark the last entry erased */ - - /* todo:consider a 2 stage erase or write before erase and do a fs check - * at start up - */ - - rv = erase_entry(pf); - - if (rv < 0) { - return rv; - } - - pf = (flash_entry_header_t *) current_sector->address; - - if (!blank_check(pf, total_size)) { - rv = erase_sector(current_sector, pf); - } - }//free_space_in_sector returned not-zero + /* Append after the last CRC-valid record. Compaction is the caller's job. */ + flash_entry_header_t *pf = find_append(total_size); + if (pf == 0) { + return -ENOSPC; } flash_entry_header_t *pn = (flash_entry_header_t *)(buffer - sizeof(flash_entry_header_t)); @@ -1108,80 +962,101 @@ int parameter_flashfs_erase(void) /**************************************************************************** - * Name: parameter_flashfs_compact_if_full - * - * Description: - * If the sector cannot take another entry the size of the one it holds - * (plus a growth margin), rewrite that entry now through the normal write - * path, which wraps and erases the sector. - * - * An erase stalls every read of the bank the sector lives in until it - * completes, which for a 128 KB sector on STM32H7 is on the order of a - * second. Where the firmware image shares that bank, execution halts for - * the duration. A save can be triggered at any time - including the - * autosave released on disarm - so paying the wrap here bounds that halt - * to a point where the vehicle is not yet armed and nothing is being - * controlled. In-service saves then stay append-only (a few 32-byte row - * programs). If the stored data outgrows the margin between boots, the - * save-time wrap still covers it. - * - * Cost: a boot that compacts takes one erase longer to reach the rest of - * board init. Every other boot pays only the free-space check below. - * - * Returned value: - * 1 if a compaction (sector erase + rewrite) was performed, 0 if there - * was enough space or nothing is stored, or a negative errno. - * + * Name: parameter_flashfs_walk ****************************************************************************/ -int parameter_flashfs_compact_if_full(void) +struct walk_pack { + parameter_flashfs_walk_cb cb; + void *arg; +}; + +static int walk_one(flash_entry_header_t *pf, void *arg) +{ + struct walk_pack *w = (struct walk_pack *)arg; + return w->cb(entry_data(pf), entry_data_length(pf), w->arg); +} + +int parameter_flashfs_walk(flash_file_token_t token, parameter_flashfs_walk_cb cb, void *arg) +{ + struct walk_pack w = { cb, arg }; + return for_each_valid_entry(token, walk_one, &w); +} + +/**************************************************************************** + * Name: parameter_flashfs_needs_compact + ****************************************************************************/ + +size_t parameter_flashfs_max_payload(void) +{ + if (sector_map == NULL) { + return 0; + } + + size_t max_total = 0; + + for (int s = 0; sector_map[s].address; s++) { + if (sector_usable(s) > max_total) { + max_total = sector_usable(s); + } + } + + /* h_size_t holds the record size */ + if (max_total > UINT16_MAX) { + max_total = UINT16_MAX; + } + + max_total &= ~(size_t)SizeMask; + + if (max_total <= sizeof(flash_entry_header_t)) { + return 0; + } + + return max_total - sizeof(flash_entry_header_t); +} + +int parameter_flashfs_needs_compact(flash_file_token_t token, size_t snapshot_size) { if (sector_map == NULL) { return -ENXIO; } - flash_entry_header_t *pf = find_entry(parameters_token); + flash_entry_header_t *first = find_entry(token); - if (pf == NULL) { + if (first == NULL) { return 0; } - /* Headroom kept beyond one more copy of the current entry. Each save - * stores the same parameter set give or take a few values, so a couple - * of KB covers normal growth over a run of saves; data that outgrows it - * takes the save-time wrap once, as before. - */ + /* Dead records ahead of the live data (a torn write, or the erased + * prefix the single-snapshot writer left) only go away with an erase. */ + if ((uintptr_t)first != (uintptr_t)sector_map[0].address) { + return 1; + } - const size_t growth_margin = 2048; + /* Keep room after the log for a burst the size of the snapshot + * (reset-all tombstones, airframe PARAM_SET), floored so a UUID append + * does not force a boot erase. */ + const size_t align = SizeMask; + const size_t snapshot_total = (snapshot_size + sizeof(flash_entry_header_t) + align) & ~align; + const size_t headroom = snapshot_total > 8192 ? snapshot_total : 8192; - size_t data_length = entry_data_length(pf); - size_t entry_total = sizeof(flash_entry_header_t) + data_length + SizeMask; - size_t reserve = entry_total + growth_margin; - - if (check_free_space_in_sector(pf, reserve) == NULL) { + if (find_append(headroom) != NULL) { return 0; } - /* The entry data lives in the flash about to be erased: copy it out first. */ - - uint8_t *buffer; - size_t buffer_size = data_length; - int rv = parameter_flashfs_alloc(parameters_token, &buffer, &buffer_size); - - if (rv != 0) { - return rv; + /* Compaction leaves the snapshot at the start of sector 0 and every + * other sector blank. Erase only if that layout has the room this one + * lacks, or every boot would pay the erase for nothing. */ + if (sector_usable(0) >= snapshot_total + headroom) { + return 1; } - if (buffer_size < data_length) { - parameter_flashfs_free(); - return -ENOMEM; + for (int s = 1; sector_map[s].address; s++) { + if (sector_usable(s) >= headroom) { + return 1; + } } - memcpy(buffer, entry_data(pf), data_length); - rv = parameter_flashfs_write(parameters_token, buffer, data_length); - parameter_flashfs_free(); - - return rv >= 0 ? 1 : rv; + return 0; } /**************************************************************************** @@ -1227,27 +1102,9 @@ int parameter_flashfs_init(sector_descriptor_t *fconfig, uint8_t *buffer, uint16 /* Sanity check */ - flash_entry_header_t *pf = find_entry(parameters_token); - - /* No paramaters */ - - if (pf == NULL) { - // Parameters can't be found, assume sector is corrupt or empty - rv = parameter_flashfs_erase(); - - // A positive return value means flash space has been erased successfully. - if (rv > 0) { - rv = 0; - } - - } else { - // Pay the sector-wrap erase here at boot, not on a save at the disarm edge. - int compacted = parameter_flashfs_compact_if_full(); - - if (compacted < 0) { - rv = compacted; - } - } + /* A non-blank store with no valid entry is torn or foreign; import + * reports -EILSEQ. Erasing here would hide a successful retry after + * a torn compact write. */ return rv; } @@ -1283,6 +1140,7 @@ __EXPORT void test(void) int rv = 0; for (int a = 0; a <= 4; a++) { + parameter_flashfs_erase(); rv = parameter_flashfs_alloc(parameters_token, &fbuffer, &buf_size); memcpy(fbuffer, test_buf, a); buf_size = a; @@ -1299,6 +1157,7 @@ __EXPORT void test(void) int block = 2048; for (int a = 0; a <= 8; a++) { + parameter_flashfs_erase(); rv = parameter_flashfs_alloc(parameters_token, &fbuffer, &buf_size); memcpy(fbuffer, test_buf, block); buf_size = block; diff --git a/src/lib/parameters/flashparams/flashparams.cpp b/src/lib/parameters/flashparams/flashparams.cpp index ba055dd5bdb..17cceb442ec 100644 --- a/src/lib/parameters/flashparams/flashparams.cpp +++ b/src/lib/parameters/flashparams/flashparams.cpp @@ -75,102 +75,307 @@ struct param_wbuf_s { param_t param; }; -static int -param_export_internal(param_filter_func filter) +/* + * Same-bank program/erase stalls instruction fetch on STM32H7. A full + * snapshot rewrite is tens of ms; a 128 KB sector erase is ~1 s. In-service + * saves therefore append a delta of unsaved params (a few 32-byte rows). + * At boot, before sensors or DShot start, compact to one snapshot only when + * the sector cannot fit a burst of deltas, so a disarm-edge UUID save does + * not pay the erase every flight. + * + * Firmware before the log format wrote one parameters_legacy_token snapshot + * and cannot replay deltas. It ignores parameters_token records and treats + * a store without its own as empty (STM32H7 erases it at init), so a + * downgrade boots on defaults instead of a partial set. A legacy record is + * always the newest state, since only that firmware writes one: import it + * alone and rewrite the store as a log-format snapshot. + */ + +/* Replay met a record whose name or type is not in this firmware's parameter + * table, so it is usable through a translation at best. The file backend + * drops those on its next save; here they stay until a compaction, and a + * translation is only kept for a few releases. A snapshot cannot hold such a + * record, so compacting for it does not repeat. */ +static bool log_stale; + +/* Replay failed inside a CRC-valid record. A delta appended behind it would + * never be reached, so the next save rewrites the store. */ +static bool log_unreadable; + +static bool +any_unsaved(param_filter_func filter) { - bson_encoder_s encoder{}; - int result = -1; - - /* Use realloc */ - - bson_encoder_init_buf(&encoder, nullptr, 0); - auto changed_params = user_config.containedAsBitset(); - for (param_t param = 0; param < user_config.PARAM_COUNT; param++) { + if ((filter == nullptr || filter(param)) && param_value_unsaved(param)) { + return true; + } + } - if (!changed_params[param] || (filter && !filter(param))) { + return false; +} + +/* + * A snapshot holds every override in RAM, equal to the current default or + * not: the boot compaction runs before the airframe applies its set-default + * values, so at that point "equals the default" says nothing. A delta holds + * the unsaved params; one that no longer overrides, or now matches the + * default in force, is written as a BSON null so replay drops it, which is + * what the file backend's "don't export defaults" gives after a reload. + */ +static int +encode_params(bson_encoder_s *encoder, param_filter_func filter, bool snapshot) +{ + for (param_t param = 0; param < user_config.PARAM_COUNT; param++) { + if (filter && !filter(param)) { continue; } - int32_t i; - float f; + if (snapshot) { + if (!user_config.contains(param)) { + continue; + } - /* append the appropriate BSON type object */ + } else { + if (!param_value_unsaved(param)) { + continue; + } + + if (!user_config.contains(param) || param_value_is_default(param)) { + if (bson_encoder_append_null(encoder, param_name(param))) { + debug("BSON append failed for '%s'", param_name(param)); + return -1; + } + + continue; + } + } switch (param_type(param)) { case PARAM_TYPE_INT32: - i = user_config.get(param).i; - - if (bson_encoder_append_int32(&encoder, param_name(param), i)) { - debug("BSON append failed for '%s'", param_name(s->param)); - goto out; + if (bson_encoder_append_int32(encoder, param_name(param), user_config.get(param).i)) { + debug("BSON append failed for '%s'", param_name(param)); + return -1; } break; case PARAM_TYPE_FLOAT: - f = user_config.get(param).f; - - if (bson_encoder_append_double(&encoder, param_name(param), (double)f)) { - debug("BSON append failed for '%s'", param_name(s->param)); - goto out; + if (bson_encoder_append_double(encoder, param_name(param), (double)user_config.get(param).f)) { + debug("BSON append failed for '%s'", param_name(param)); + return -1; } break; default: debug("unrecognized parameter type"); - goto out; + return -1; } } - result = 0; + return 0; +} -out: +/* The caller frees bson_encoder_buf_data() whatever the result. */ +static int +encode_document(bson_encoder_s *encoder, param_filter_func filter, bool snapshot) +{ + bson_encoder_init_buf(encoder, nullptr, 0); + + int result = encode_params(encoder, filter, snapshot); if (result == 0) { - - /* Finalize the bison encoding*/ - - bson_encoder_fini(&encoder); - - /* Get requiered space */ - - size_t buf_size = bson_encoder_buf_size(&encoder); - - /* Get a buffer from the flash driver with enough space */ - - uint8_t *buffer; - result = parameter_flashfs_alloc(parameters_token, &buffer, &buf_size); - - if (result == OK) { - - /* Check for a write that has no changes */ - - uint8_t *was_buffer; - size_t was_buf_size; - int was_result = parameter_flashfs_read(parameters_token, &was_buffer, &was_buf_size); - - void *enc_buff = bson_encoder_buf_data(&encoder); - - bool commit = was_result < OK || was_buf_size != buf_size || 0 != memcmp(was_buffer, enc_buff, was_buf_size); - - if (commit) { - - memcpy(buffer, enc_buff, buf_size); - result = parameter_flashfs_write(parameters_token, buffer, buf_size); - result = result == buf_size ? OK : -EFBIG; - - } - - free(enc_buff); - parameter_flashfs_free(); - } + result = bson_encoder_fini(encoder); } return result; } +static int +stage_write_buffer(bson_encoder_s *encoder, uint8_t **buffer, size_t *enc_size) +{ + void *enc_buff = bson_encoder_buf_data(encoder); + *enc_size = bson_encoder_buf_size(encoder); + + if (enc_buff == nullptr) { + return -ENOMEM; + } + + size_t alloc_size = *enc_size; + int result = parameter_flashfs_alloc(parameters_token, buffer, &alloc_size); + + if (result != OK) { + return result; + } + + if (alloc_size < *enc_size) { + parameter_flashfs_free(); + return -ENOMEM; + } + + memcpy(*buffer, enc_buff, *enc_size); + return OK; +} + +static int +commit_append(uint8_t *buffer, size_t enc_size) +{ + int result = parameter_flashfs_write(parameters_token, buffer, enc_size); + parameter_flashfs_free(); + + if (result == (int)enc_size) { + return OK; + } + + return result < 0 ? result : -EFBIG; +} + +static int +export_delta(param_filter_func filter) +{ + bson_encoder_s encoder{}; + int result = encode_document(&encoder, filter, false); + + if (result == 0) { + uint8_t *buffer; + size_t enc_size; + result = stage_write_buffer(&encoder, &buffer, &enc_size); + + if (result == OK) { + result = commit_append(buffer, enc_size); + } + } + + free(bson_encoder_buf_data(&encoder)); + return result; +} + +/* Everything that can fail before the erase, so a failure here leaves the + * log untouched and still loadable. */ +static int +stage_snapshot(bson_encoder_s *encoder, uint8_t **buffer, size_t *enc_size) +{ + int result = stage_write_buffer(encoder, buffer, enc_size); + + if (result != OK) { + return result; + } + + if (*enc_size > parameter_flashfs_max_payload()) { + parameter_flashfs_free(); + return -ENOSPC; + } + + return OK; +} + +static int +commit_snapshot(uint8_t *buffer, size_t enc_size) +{ + if (parameter_flashfs_blank() != 1) { + int rv = parameter_flashfs_erase(); + + if (rv < 0) { + parameter_flashfs_free(); + return rv; + } + } + + int result = parameter_flashfs_write(parameters_token, buffer, enc_size); + + if (result != (int)enc_size && parameter_flashfs_blank() == 1) { + result = parameter_flashfs_write(parameters_token, buffer, enc_size); + } + + parameter_flashfs_free(); + + if (result == (int)enc_size) { + return OK; + } + + return result < 0 ? result : -EFBIG; +} + +static int +compact_to_snapshot() +{ + bson_encoder_s encoder{}; + int result = encode_document(&encoder, nullptr, true); + + if (result == 0) { + uint8_t *buffer; + size_t enc_size; + result = stage_snapshot(&encoder, &buffer, &enc_size); + + if (result == OK) { + result = commit_snapshot(buffer, enc_size); + } + } + + if (result == OK) { + log_unreadable = false; + } + + free(bson_encoder_buf_data(&encoder)); + return result; +} + +static bool +has_record(flash_file_token_t token) +{ + uint8_t *buffer = nullptr; + size_t buf_size = 0; + return parameter_flashfs_read(token, &buffer, &buf_size) >= 0; +} + +static int +param_export_internal(param_filter_func filter) +{ + /* A delta only means something behind a snapshot that replays. A legacy + * record still present means the boot migration did not run; replay + * would take it over any delta appended behind it. */ + if (log_unreadable || has_record(parameters_legacy_token) || !has_record(parameters_token)) { + return compact_to_snapshot(); + } + + if (!any_unsaved(filter)) { + return 0; + } + + int result = export_delta(filter); + + if (result != -ENOSPC) { + return result; + } + + /* Same-bank erase stalls the CPU for ~1 s; last resort when a single + * save does not fit the tail. Compact writes the full RAM snapshot so a + * filtered save cannot drop params not in the delta. */ + PX4_WARN("flashparams: param sector full, compacting"); + return compact_to_snapshot(); +} + + +static bool +record_is_stale(bson_node_t node) +{ + const param_t param = param_find_no_notification(node->name); + + if (param == PARAM_INVALID) { + return true; + } + + switch (node->type) { + case BSON_INT32: + return param_type(param) != PARAM_TYPE_INT32; + + case BSON_DOUBLE: + return param_type(param) != PARAM_TYPE_FLOAT; + + default: + return false; + } +} static int param_import_callback(bson_decoder_t decoder, bson_node_t node) @@ -189,8 +394,18 @@ param_import_callback(bson_decoder_t decoder, bson_node_t node) return 0; } - if (param_modify_on_import(node) == param_modify_on_import_ret::PARAM_SKIP_IMPORT) { - return 1; + /* A tombstone has no value. The decoder leaves the previous node's in + * the union and the translations read it without a type check. */ + const bool tombstone = node->type == BSON_nullptr || node->type == BSON_UNDEFINED; + + if (!tombstone) { + if (record_is_stale(node)) { + log_stale = true; + } + + if (param_modify_on_import(node) == param_modify_on_import_ret::PARAM_SKIP_IMPORT) { + return 1; + } } /* @@ -209,6 +424,12 @@ param_import_callback(bson_decoder_t decoder, bson_node_t node) */ switch (node->type) { + case BSON_nullptr: + case BSON_UNDEFINED: + user_config.reset(param); + result = 1; + goto out; + case BSON_INT32: if (param_type(param) != PARAM_TYPE_INT32) { PX4_WARN("unexpected type for %s", node->name); @@ -250,16 +471,44 @@ out: } static int -param_import_internal() +import_one_entry(uint8_t *buffer, size_t buf_size, void *arg) { + int *result = static_cast(arg); bson_decoder_s decoder{}; - int result = -1; - uint8_t *buffer = nullptr; - size_t buf_size = 0; - int read_result = parameter_flashfs_read(parameters_token, &buffer, &buf_size); + if (bson_decoder_init_buf(&decoder, buffer, buf_size, param_import_callback)) { + debug("decoder init failed"); + *result = -1; + return -1; + } - if (read_result == -ENOENT || (read_result >= 0 && (buffer == nullptr || buf_size == 0))) { + do { + *result = bson_decoder_next(&decoder); + + } while (*result > 0); + + return *result < 0 ? *result : 0; +} + +static int +param_import_internal(bool *legacy) +{ + int result = 0; + log_stale = false; + int n = parameter_flashfs_walk(parameters_legacy_token, import_one_entry, &result); + *legacy = n > 0; + + if (n == 0) { + n = parameter_flashfs_walk(parameters_token, import_one_entry, &result); + } + + if (n < 0) { + debug("flash walk failed (%d)", n); + log_unreadable = true; + return n; + } + + if (n == 0) { /* No valid entry found. A fully erased store is blank (first boot, or * after switching firmware): report "not yet stored" (1), matching the * convention used by param_load_default(). A store that holds data but @@ -273,27 +522,58 @@ param_import_internal() return -EILSEQ; } - if (read_result < 0) { - debug("flash read failed (%d)", read_result); - return read_result; - } - - if (bson_decoder_init_buf(&decoder, buffer, buf_size, param_import_callback)) { - debug("decoder init failed"); - goto out; - } - - do { - result = bson_decoder_next(&decoder); - - } while (result > 0); - -out: - if (result < 0) { debug("BSON error decoding parameters"); + return result; } + return 0; +} + +/* + * Same-bank erase stalls instruction fetch for ~1 s on H7. Do it here at + * boot, before sensors or DShot start, and only when a burst of deltas would + * no longer fit, the store is still in the legacy layout, or the log holds + * records this firmware translated or could not use. Only a failure after + * the erase is an error: until then the log is intact and RAM holds what it + * said, so the boot must not be reported as a corrupt store. + */ +static int +compact_after_import(bool legacy) +{ + bson_encoder_s encoder{}; + int result = encode_document(&encoder, nullptr, true); + + if (result != 0) { + PX4_ERR("flashparams: snapshot encode failed (%d), log kept", result); + free(bson_encoder_buf_data(&encoder)); + return 0; + } + + const size_t enc_size = bson_encoder_buf_size(&encoder); + int need = (legacy || log_stale) ? 1 : parameter_flashfs_needs_compact(parameters_token, enc_size); + result = 0; + + if (need > 0) { + uint8_t *buffer; + size_t staged_size; + result = stage_snapshot(&encoder, &buffer, &staged_size); + + if (result != OK) { + PX4_ERR("flashparams: cannot stage snapshot (%d), log kept", result); + result = 0; + + } else { + PX4_INFO("flashparams: compacting param sector%s", legacy ? " (legacy layout)" : ""); + result = commit_snapshot(buffer, staged_size); + + if (result != OK) { + PX4_ERR("flashparams: compact failed (%d)", result); + } + } + } + + free(bson_encoder_buf_data(&encoder)); return result; } @@ -302,13 +582,14 @@ int flash_param_save(param_filter_func filter) return param_export_internal(filter); } -int flash_param_load() -{ - param_reset_all(); - return param_import_internal(); -} - int flash_param_import() { - return param_import_internal(); + bool legacy = false; + int result = param_import_internal(&legacy); + + if (result != 0) { + return result; + } + + return compact_after_import(legacy); } diff --git a/src/lib/parameters/flashparams/flashparams.h b/src/lib/parameters/flashparams/flashparams.h index a64b74de19c..d3afedbd493 100644 --- a/src/lib/parameters/flashparams/flashparams.h +++ b/src/lib/parameters/flashparams/flashparams.h @@ -64,7 +64,6 @@ __EXPORT void param_get_external(param_t param, void *val); /* The interface hooks to the Flash based storage. The caller is responsible for locking */ __EXPORT int flash_param_save(param_filter_func filter); -__EXPORT int flash_param_load(); __EXPORT int flash_param_import(); __END_DECLS diff --git a/src/lib/parameters/parameters.cpp b/src/lib/parameters/parameters.cpp index aa454afe12f..a03b232ec7e 100644 --- a/src/lib/parameters/parameters.cpp +++ b/src/lib/parameters/parameters.cpp @@ -92,7 +92,6 @@ using namespace time_literals; #include "flashparams/flashparams.h" #else inline static int flash_param_save(param_filter_func filter) { return -1; } -inline static int flash_param_load() { return -1; } inline static int flash_param_import() { return -1; } #endif @@ -105,6 +104,12 @@ static ParamAutosave *autosave_instance {nullptr}; static px4::AtomicBitset params_active; // params found static px4::AtomicBitset params_unsaved; +// Params the save in progress is writing. They are moved out of params_unsaved +// before their values are read, so a param_set that lands while the save runs +// marks its param again and reaches the next save. Flash-param saves write only +// unsaved params, so nothing else would ever write that change. +static px4::AtomicBitset params_saving; + static ConstLayer firmware_defaults; static DynamicSparseLayer runtime_defaults{&firmware_defaults}; DynamicSparseLayer user_config{&runtime_defaults}; @@ -290,7 +295,7 @@ int param_get_used_index(param_t param) bool param_value_unsaved(param_t param) { - return handle_in_range(param) ? params_unsaved[param] : false; + return handle_in_range(param) ? (params_unsaved[param] || params_saving[param]) : false; } int @@ -411,7 +416,8 @@ param_control_autosave(bool enable) } static int -param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_changes, bool update_remote = true) +param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_changes, bool update_remote = true, + bool autosave = true) { if (!handle_in_range(param)) { PX4_ERR("set invalid param %d", param); @@ -453,7 +459,10 @@ param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_ } if (user_config.store(param, new_value)) { - params_unsaved.set(param, !mark_saved && param_changed); + if (param_changed) { + params_unsaved.set(param, !mark_saved); + } + result = PX4_OK; } else { @@ -461,7 +470,7 @@ param_set_internal(param_t param, const void *val, bool mark_saved, bool notify_ result = PX4_ERROR; } - if ((result == PX4_OK) && param_changed && !mark_saved) { // this is false when importing parameters + if ((result == PX4_OK) && param_changed && !mark_saved && autosave) { // this is false when importing parameters param_autosave(); } @@ -645,6 +654,13 @@ static int param_reset_internal(param_t param, bool notify = true, bool autosave user_config.reset(param); } + if (param_found) { + /* Persist the reset. Flash-param saves append only unsaved params, + * so a reset has to show up as a tombstone or replay keeps the + * previous override. */ + params_unsaved.set(param, true); + } + if (autosave) { param_autosave(); } @@ -830,6 +846,13 @@ int param_save_default(bool blocking) int res = PX4_ERROR; const char *filename = param_get_default_file(); + for (param_t param = 0; handle_in_range(param); param++) { + if (params_unsaved[param]) { + params_saving.set(param, true); + params_unsaved.set(param, false); + } + } + if (filename) { static constexpr int MAX_ATTEMPTS = 3; @@ -869,9 +892,16 @@ int param_save_default(bool blocking) if (res != PX4_OK) { PX4_ERR("param export failed (%d)", res); - } else { - params_unsaved.reset(); + for (param_t param = 0; handle_in_range(param); param++) { + if (params_saving[param]) { + params_unsaved.set(param, true); + } + } + } + params_saving.reset(); + + if (res == PX4_OK) { // backup file if (param_backup_file) { int fd_backup_file = ::open(param_backup_file, O_WRONLY | O_CREAT | O_TRUNC, PX4_O_MODE_666); @@ -912,7 +942,7 @@ param_load_default() const char *filename = param_get_default_file(); if (!filename) { - return flash_param_load(); + return param_load(-1); } int fd_load = ::open(filename, O_RDONLY); @@ -1179,6 +1209,14 @@ out: return result; } +#if defined(FLASH_BASED_PARAMS) +// A file is never the flash backend's store, so what an import changes is unsaved until a delta carries it. +// As on the file backend, the import itself does not save. +static constexpr bool file_import_is_saved = false; +#else +static constexpr bool file_import_is_saved = true; +#endif + static int param_import_callback(bson_decoder_t decoder, bson_node_t node) { @@ -1191,8 +1229,12 @@ param_import_callback(bson_decoder_t decoder, bson_node_t node) return 0; } + // A tombstone has no value. The decoder leaves the previous node's in the union and the translations + // read it without a type check. + const bool tombstone = node->type == BSON_nullptr || node->type == BSON_UNDEFINED; + // if we do param_set() directly in the translation, set PARAM_SKIP_IMPORT as return value and return here - if (param_modify_on_import(node) == param_modify_on_import_ret::PARAM_SKIP_IMPORT) { + if (!tombstone && param_modify_on_import(node) == param_modify_on_import_ret::PARAM_SKIP_IMPORT) { return 1; } @@ -1206,10 +1248,15 @@ param_import_callback(bson_decoder_t decoder, bson_node_t node) // Handle setting the parameter from the node switch (node->type) { + case BSON_nullptr: + case BSON_UNDEFINED: + param_reset_internal(param, true, false); + return 1; + case BSON_INT32: { if (param_type(param) == PARAM_TYPE_INT32) { int32_t i = node->i32; - param_set_internal(param, &i, true, true); + param_set_internal(param, &i, file_import_is_saved, true, true, false); PX4_DEBUG("Imported %s with value %" PRIi32, param_name(param), i); } else { @@ -1221,7 +1268,7 @@ param_import_callback(bson_decoder_t decoder, bson_node_t node) case BSON_DOUBLE: { if (param_type(param) == PARAM_TYPE_FLOAT) { float f = node->d; - param_set_internal(param, &f, true, true); + param_set_internal(param, &f, file_import_is_saved, true, true, false); PX4_DEBUG("Imported %s with value %f", param_name(param), (double)f); } else { @@ -1323,11 +1370,54 @@ param_import_internal(int fd) return -1; } +#if defined(FLASH_BASED_PARAMS) +static int flash_backend_locked(int (*op)()) +{ + /* Same order as param_save_default: mutex then shutdown lock. Autosave + * uses a trylock, so a compact holds it off the flash programming. */ + pthread_mutex_lock(&file_mutex); + + int shutdown_lock_ret = px4_shutdown_lock(); + + if (shutdown_lock_ret != 0) { + PX4_ERR("px4_shutdown_lock() failed (%i)", shutdown_lock_ret); + } + + int result = op(); + + pthread_mutex_unlock(&file_mutex); + + if (shutdown_lock_ret == 0) { + px4_shutdown_unlock(); + } + + return result; +} + +static int flash_load_locked() +{ + /* Reset under the lock so a pending autosave cannot append tombstones + * of the just-cleared RAM before import/compact run. */ + param_reset_all_internal(false); + int result = flash_param_import(); + + if (result == 0 || result == 1) { + params_unsaved.reset(); + } + + return result; +} +#endif + int param_import(int fd) { if (fd < 0) { +#if defined(FLASH_BASED_PARAMS) + return flash_backend_locked(flash_param_import); +#else return flash_param_import(); +#endif } return param_import_internal(fd); @@ -1337,11 +1427,25 @@ int param_load(int fd) { if (fd < 0) { - return flash_param_load(); +#if defined(FLASH_BASED_PARAMS) + return flash_backend_locked(flash_load_locked); +#else + return PX4_ERROR; +#endif } param_reset_all_internal(false); - return param_import_internal(fd); + int result = param_import_internal(fd); + +#if !defined(FLASH_BASED_PARAMS) + + if (result >= 0) { + params_unsaved.reset(); + } + +#endif + + return result; } void diff --git a/src/lib/tinybson/tinybson.cpp b/src/lib/tinybson/tinybson.cpp index 04949266df2..9bf5cfd5551 100644 --- a/src/lib/tinybson/tinybson.cpp +++ b/src/lib/tinybson/tinybson.cpp @@ -319,6 +319,10 @@ bson_decoder_next(bson_decoder_t decoder) decoder->count_node_int64++; break; + case BSON_nullptr: + case BSON_UNDEFINED: + break; + /* XXX currently not supporting other types */ default: CODER_KILL(decoder, "unsupported node type"); @@ -528,9 +532,9 @@ bson_encoder_fini(bson_encoder_t encoder) } } - // record document size - debug("writing document size %" PRIi32, encoder->total_document_size); - const int32_t bson_doc_bytes = encoder->total_document_size; + // record document size; total_document_size only counts what went to a file + const int32_t bson_doc_bytes = (encoder->fd > -1) ? encoder->total_document_size : (int32_t)encoder->bufpos; + debug("writing document size %" PRIi32, bson_doc_bytes); if (encoder->fd > -1) { if ((lseek(encoder->fd, 0, SEEK_SET) != 0) @@ -663,3 +667,16 @@ bson_encoder_append_binary(bson_encoder_t encoder, const char *name, bson_binary return 0; } + +int +bson_encoder_append_null(bson_encoder_t encoder, const char *name) +{ + CODER_CHECK(encoder); + + if (write_int8(encoder, BSON_nullptr) || + write_name(encoder, name)) { + CODER_KILL(encoder, "write error on BSON_nullptr"); + } + + return 0; +} diff --git a/src/lib/tinybson/tinybson.h b/src/lib/tinybson/tinybson.h index eb370454eed..26950a98867 100644 --- a/src/lib/tinybson/tinybson.h +++ b/src/lib/tinybson/tinybson.h @@ -303,5 +303,10 @@ __EXPORT int bson_encoder_append_string(bson_encoder_t encoder, const char *name __EXPORT int bson_encoder_append_binary(bson_encoder_t encoder, const char *name, bson_binary_subtype_t subtype, size_t size, const void *data); +/** + * Append a BSON null (type 0x0A). Name only; no value payload. + */ +__EXPORT int bson_encoder_append_null(bson_encoder_t encoder, const char *name); + #endif