storage.h
1 /*
2  * SPDX-FileCopyrightText: 2020 Koen Zandberg
3  * SPDX-FileCopyrightText: 2020 Inria
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
98 #include "suit.h"
99 
100 #ifdef __cplusplus
101 extern "C" {
102 #endif
103 
107 typedef struct suit_storage suit_storage_t;
108 
112 typedef struct suit_storage_driver {
113 
119  int (*init)(suit_storage_t *storage);
120 
131  int (*start)(suit_storage_t *storage, const suit_manifest_t *manifest,
132  size_t len);
133 
146  int (*write)(suit_storage_t *storage, const suit_manifest_t *manifest, const
147  uint8_t *buf, size_t offset, size_t len);
148 
158  int (*finish)(suit_storage_t *storage, const suit_manifest_t *manifest);
159 
171  int (*read)(suit_storage_t *storage, uint8_t *buf, size_t offset,
172  size_t len);
173 
186  int (*read_ptr)(suit_storage_t *storage,
187  const uint8_t **buf, size_t *len);
188 
198  int (*install)(suit_storage_t *storage, const suit_manifest_t *manifest);
199 
208  int (*erase)(suit_storage_t *storage);
209 
219  bool (*has_location)(const suit_storage_t *storage, const char *location);
220 
234  bool (*match_offset)(const suit_storage_t *storage, size_t offset);
235 
254  int (*set_active_location)(suit_storage_t *storage, const char *location);
255 
268  int (*get_seq_no)(const suit_storage_t *storage, uint32_t *seq_no);
269 
279  int (*set_seq_no)(suit_storage_t *storage, uint32_t seq_no);
280 
284  char separator;
286 
290 struct suit_storage {
292 };
293 
303 
315  const suit_component_t *component);
316 
321 
330 int suit_storage_get_highest_seq_no(uint32_t *seq_no);
331 
341 int suit_storage_set_seq_no_all(uint32_t seq_no);
342 
357 static inline char suit_storage_get_separator(const suit_storage_t *storage)
358 {
359  return storage->driver->separator;
360 }
361 
371 static inline bool suit_storage_has_readptr(const suit_storage_t *storage)
372 {
373  return (storage->driver->read_ptr);
374 }
375 
385 static inline bool suit_storage_has_offset(const suit_storage_t *storage)
386 {
387  return (storage->driver->match_offset);
388 }
389 
395 static inline int suit_storage_init(suit_storage_t *storage)
396 {
397  return storage->driver->init(storage);
398 }
399 
410 static inline int suit_storage_start(suit_storage_t *storage,
411  const suit_manifest_t *manifest,
412  size_t len)
413 {
414  return storage->driver->start(storage, manifest, len);
415 }
416 
429 static inline int suit_storage_write(suit_storage_t *storage,
430  const suit_manifest_t *manifest,
431  const uint8_t *buf, size_t offset,
432  size_t len)
433 {
434  return storage->driver->write(storage, manifest, buf, offset, len);
435 }
436 
446 static inline int suit_storage_finish(suit_storage_t *storage,
447  const suit_manifest_t *manifest)
448 {
449  return storage->driver->finish(storage, manifest);
450 }
451 
463 static inline int suit_storage_read(suit_storage_t *storage, uint8_t *buf,
464  size_t offset, size_t len)
465 {
466  return storage->driver->read(storage, buf, offset, len);
467 }
468 
481 static inline int suit_storage_read_ptr(suit_storage_t *storage, const uint8_t
482  **buf, size_t *len)
483 {
484  return storage->driver->read_ptr(storage, buf, len);
485 }
486 
496 static inline int suit_storage_install(suit_storage_t *storage,
497  const suit_manifest_t *manifest)
498 {
499  return storage->driver->install(storage, manifest);
500 }
501 
510 static inline int suit_storage_erase(suit_storage_t *storage)
511 {
512  return storage->driver->erase(storage);
513 }
514 
524 static inline bool suit_storage_has_location(suit_storage_t *storage,
525  const char *location)
526 {
527  return storage->driver->has_location(storage, location);
528 }
529 
543 static inline bool suit_storage_match_offset(const suit_storage_t *storage,
544  size_t offset)
545 {
546  return storage->driver->match_offset(storage, offset);
547 }
548 
568  const char *location)
569 {
570  return storage->driver->set_active_location(storage, location);
571 }
572 
585 static inline int suit_storage_get_seq_no(const suit_storage_t *storage,
586  uint32_t *seq_no)
587 {
588  return storage->driver->get_seq_no(storage, seq_no);
589 }
590 
600 static inline int suit_storage_set_seq_no(suit_storage_t *storage,
601  uint32_t seq_no)
602 {
603  return storage->driver->set_seq_no(storage, seq_no);
604 }
607 #ifdef __cplusplus
608 }
609 #endif
610 
static int suit_storage_read(suit_storage_t *storage, uint8_t *buf, size_t offset, size_t len)
Read a chunk of previously written data back.
Definition: storage.h:463
suit_storage_t * suit_storage_find_by_component(const suit_manifest_t *manifest, const suit_component_t *component)
retrieve a storage backend based on the suit component
suit_storage_t * suit_storage_find_by_id(const char *id)
retrieve a storage backend based on the location ID string
static bool suit_storage_has_location(suit_storage_t *storage, const char *location)
Check if this storage backend services a location.
Definition: storage.h:524
static int suit_storage_get_seq_no(const suit_storage_t *storage, uint32_t *seq_no)
Retrieve the sequence number from the storage backend.
Definition: storage.h:585
static char suit_storage_get_separator(const suit_storage_t *storage)
get the separator for a storage backend
Definition: storage.h:357
static int suit_storage_install(suit_storage_t *storage, const suit_manifest_t *manifest)
Install the payload or mark the payload as valid.
Definition: storage.h:496
static int suit_storage_read_ptr(suit_storage_t *storage, const uint8_t **buf, size_t *len)
retrieve a direct read pointer for this storage backend
Definition: storage.h:481
static bool suit_storage_has_readptr(const suit_storage_t *storage)
Check if the storage backend implements the suit_storage_driver_t::read_ptr function.
Definition: storage.h:371
static bool suit_storage_match_offset(const suit_storage_t *storage, size_t offset)
Checks if the supplied offset is true or false for the current location.
Definition: storage.h:543
static bool suit_storage_has_offset(const suit_storage_t *storage)
Check if the storage backend implements the suit_storage_driver_t::match_offset function.
Definition: storage.h:385
static int suit_storage_erase(suit_storage_t *storage)
Erase the previously loaded payload.
Definition: storage.h:510
static int suit_storage_set_seq_no(suit_storage_t *storage, uint32_t seq_no)
Set a new sequence number in the storage backend.
Definition: storage.h:600
static int suit_storage_start(suit_storage_t *storage, const suit_manifest_t *manifest, size_t len)
Start a new payload write sequence.
Definition: storage.h:410
void suit_storage_init_all(void)
initialize all storage backends
static int suit_storage_init(suit_storage_t *storage)
One-time initialization function.
Definition: storage.h:395
static int suit_storage_finish(suit_storage_t *storage, const suit_manifest_t *manifest)
Signal that the payload write stage done to the storage backend.
Definition: storage.h:446
static int suit_storage_set_active_location(suit_storage_t *storage, const char *location)
Set the active location of the storage handler.
Definition: storage.h:567
int suit_storage_get_highest_seq_no(uint32_t *seq_no)
Get the highest sequence number among available backends.
int suit_storage_set_seq_no_all(uint32_t seq_no)
Set the new sequence number on all available backends.
struct suit_storage_driver suit_storage_driver_t
SUIT storage backend driver struct.
static int suit_storage_write(suit_storage_t *storage, const suit_manifest_t *manifest, const uint8_t *buf, size_t offset, size_t len)
Write a new chunk of the payload to the storage backend.
Definition: storage.h:429
SUIT component struct as decoded from the manifest.
Definition: suit.h:212
SUIT manifest struct.
Definition: suit.h:231
SUIT storage backend driver struct.
Definition: storage.h:112
int(* erase)(suit_storage_t *storage)
Erase the previously loaded payload.
Definition: storage.h:208
int(* get_seq_no)(const suit_storage_t *storage, uint32_t *seq_no)
Retrieve the sequence number from the storage backend.
Definition: storage.h:268
int(* write)(suit_storage_t *storage, const suit_manifest_t *manifest, const uint8_t *buf, size_t offset, size_t len)
Write a new chunk of the payload to the storage backend.
Definition: storage.h:146
int(* set_seq_no)(suit_storage_t *storage, uint32_t seq_no)
Set a new sequence number in the storage backend.
Definition: storage.h:279
int(* install)(suit_storage_t *storage, const suit_manifest_t *manifest)
Install the payload or mark the payload as valid.
Definition: storage.h:198
int(* start)(suit_storage_t *storage, const suit_manifest_t *manifest, size_t len)
Start a new payload write sequence.
Definition: storage.h:131
int(* set_active_location)(suit_storage_t *storage, const char *location)
Set the active location of the storage handler.
Definition: storage.h:254
bool(* has_location)(const suit_storage_t *storage, const char *location)
Check if this storage backend services a location.
Definition: storage.h:219
bool(* match_offset)(const suit_storage_t *storage, size_t offset)
Checks if the supplied offset is true or false for the current location.
Definition: storage.h:234
int(* init)(suit_storage_t *storage)
One-time initialization function.
Definition: storage.h:119
int(* finish)(suit_storage_t *storage, const suit_manifest_t *manifest)
Signal that the payload write stage done to the storage backend.
Definition: storage.h:158
char separator
Component ID separator used by this storage driver.
Definition: storage.h:284
int(* read)(suit_storage_t *storage, uint8_t *buf, size_t offset, size_t len)
Read a chunk of previously written data back.
Definition: storage.h:171
int(* read_ptr)(suit_storage_t *storage, const uint8_t **buf, size_t *len)
retrieve a direct read pointer for this storage backend
Definition: storage.h:186
Generic storage backend state.
Definition: storage.h:290
const suit_storage_driver_t * driver
Storage driver functions.
Definition: storage.h:291