storage.h
1 /*
2  * Copyright (C) 2020 Koen Zandberg
3  * 2020 Inria
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
98 #ifndef SUIT_STORAGE_H
99 #define SUIT_STORAGE_H
100 
101 #include "suit.h"
102 
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106 
111 
115 typedef struct suit_storage_driver {
116 
122  int (*init)(suit_storage_t *storage);
123 
134  int (*start)(suit_storage_t *storage, const suit_manifest_t *manifest,
135  size_t len);
136 
149  int (*write)(suit_storage_t *storage, const suit_manifest_t *manifest, const
150  uint8_t *buf, size_t offset, size_t len);
151 
161  int (*finish)(suit_storage_t *storage, const suit_manifest_t *manifest);
162 
174  int (*read)(suit_storage_t *storage, uint8_t *buf, size_t offset,
175  size_t len);
176 
189  int (*read_ptr)(suit_storage_t *storage,
190  const uint8_t **buf, size_t *len);
191 
201  int (*install)(suit_storage_t *storage, const suit_manifest_t *manifest);
202 
211  int (*erase)(suit_storage_t *storage);
212 
222  bool (*has_location)(const suit_storage_t *storage, const char *location);
223 
237  bool (*match_offset)(const suit_storage_t *storage, size_t offset);
238 
257  int (*set_active_location)(suit_storage_t *storage, const char *location);
258 
271  int (*get_seq_no)(const suit_storage_t *storage, uint32_t *seq_no);
272 
282  int (*set_seq_no)(suit_storage_t *storage, uint32_t seq_no);
283 
287  char separator;
289 
293 struct suit_storage {
295 };
296 
305 suit_storage_t *suit_storage_find_by_id(const char *id);
306 
318  const suit_component_t *component);
319 
323 void suit_storage_init_all(void);
324 
333 int suit_storage_get_highest_seq_no(uint32_t *seq_no);
334 
344 int suit_storage_set_seq_no_all(uint32_t seq_no);
345 
360 static inline char suit_storage_get_separator(const suit_storage_t *storage)
361 {
362  return storage->driver->separator;
363 }
364 
374 static inline bool suit_storage_has_readptr(const suit_storage_t *storage)
375 {
376  return (storage->driver->read_ptr);
377 }
378 
388 static inline bool suit_storage_has_offset(const suit_storage_t *storage)
389 {
390  return (storage->driver->match_offset);
391 }
392 
398 static inline int suit_storage_init(suit_storage_t *storage)
399 {
400  return storage->driver->init(storage);
401 }
402 
413 static inline int suit_storage_start(suit_storage_t *storage,
414  const suit_manifest_t *manifest,
415  size_t len)
416 {
417  return storage->driver->start(storage, manifest, len);
418 }
419 
432 static inline int suit_storage_write(suit_storage_t *storage,
433  const suit_manifest_t *manifest,
434  const uint8_t *buf, size_t offset,
435  size_t len)
436 {
437  return storage->driver->write(storage, manifest, buf, offset, len);
438 }
439 
449 static inline int suit_storage_finish(suit_storage_t *storage,
450  const suit_manifest_t *manifest)
451 {
452  return storage->driver->finish(storage, manifest);
453 }
454 
466 static inline int suit_storage_read(suit_storage_t *storage, uint8_t *buf,
467  size_t offset, size_t len)
468 {
469  return storage->driver->read(storage, buf, offset, len);
470 }
471 
484 static inline int suit_storage_read_ptr(suit_storage_t *storage, const uint8_t
485  **buf, size_t *len)
486 {
487  return storage->driver->read_ptr(storage, buf, len);
488 }
489 
499 static inline int suit_storage_install(suit_storage_t *storage,
500  const suit_manifest_t *manifest)
501 {
502  return storage->driver->install(storage, manifest);
503 }
504 
513 static inline int suit_storage_erase(suit_storage_t *storage)
514 {
515  return storage->driver->erase(storage);
516 }
517 
527 static inline bool suit_storage_has_location(suit_storage_t *storage,
528  const char *location)
529 {
530  return storage->driver->has_location(storage, location);
531 }
532 
546 static inline int suit_storage_match_offset(const suit_storage_t *storage,
547  size_t offset)
548 {
549  return storage->driver->match_offset(storage, offset);
550 }
551 
571  const char *location)
572 {
573  return storage->driver->set_active_location(storage, location);
574 }
575 
588 static inline int suit_storage_get_seq_no(const suit_storage_t *storage,
589  uint32_t *seq_no)
590 {
591  return storage->driver->get_seq_no(storage, seq_no);
592 }
593 
603 static inline int suit_storage_set_seq_no(suit_storage_t *storage,
604  uint32_t seq_no)
605 {
606  return storage->driver->set_seq_no(storage, seq_no);
607 }
610 #ifdef __cplusplus
611 }
612 #endif
613 
614 #endif /* SUIT_STORAGE_H */
615 
suit_storage_driver::read_ptr
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:189
suit_storage_driver::init
int(* init)(suit_storage_t *storage)
One-time initialization function.
Definition: storage.h:122
suit_storage_driver::erase
int(* erase)(suit_storage_t *storage)
Erase the previously loaded payload.
Definition: storage.h:211
suit_storage_set_seq_no_all
int suit_storage_set_seq_no_all(uint32_t seq_no)
Set the new sequence number on all available backends.
suit_storage_get_highest_seq_no
int suit_storage_get_highest_seq_no(uint32_t *seq_no)
Get the highest sequence number among available backends.
suit_storage_driver::start
int(* start)(suit_storage_t *storage, const suit_manifest_t *manifest, size_t len)
Start a new payload write sequence.
Definition: storage.h:134
suit_storage_driver::finish
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:161
suit_storage_install
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:499
suit_storage_driver::set_active_location
int(* set_active_location)(suit_storage_t *storage, const char *location)
Set the active location of the storage handler.
Definition: storage.h:257
suit_storage_driver::match_offset
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:237
suit_storage
Generic storage backend state.
Definition: storage.h:293
suit_storage_driver::write
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:149
suit_storage_driver::has_location
bool(* has_location)(const suit_storage_t *storage, const char *location)
Check if this storage backend services a location.
Definition: storage.h:222
suit_storage_driver::get_seq_no
int(* get_seq_no)(const suit_storage_t *storage, uint32_t *seq_no)
Retrieve the sequence number from the storage backend.
Definition: storage.h:271
suit_storage_erase
static int suit_storage_erase(suit_storage_t *storage)
Erase the previously loaded payload.
Definition: storage.h:513
suit_storage_finish
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:449
suit_storage_find_by_id
suit_storage_t * suit_storage_find_by_id(const char *id)
retrieve a storage backend based on the location ID string
suit_storage_driver::install
int(* install)(suit_storage_t *storage, const suit_manifest_t *manifest)
Install the payload or mark the payload as valid.
Definition: storage.h:201
suit_storage_driver::separator
char separator
Component ID separator used by this storage driver.
Definition: storage.h:287
suit_storage_start
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:413
suit_storage_read_ptr
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:484
suit_storage_driver::set_seq_no
int(* set_seq_no)(suit_storage_t *storage, uint32_t seq_no)
Set a new sequence number in the storage backend.
Definition: storage.h:282
suit_storage::driver
const suit_storage_driver_t * driver
Storage driver functions.
Definition: storage.h:294
suit_storage_read
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:466
suit_storage_has_offset
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:388
suit_storage_set_seq_no
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:603
suit_storage_driver::read
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:174
suit_storage_has_readptr
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:374
suit_storage_get_seq_no
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:588
suit_storage_find_by_component
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_write
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:432
suit_component_t
SUIT component struct as decoded from the manifest.
Definition: suit.h:214
suit_storage_driver
SUIT storage backend driver struct.
Definition: storage.h:115
suit_storage_driver_t
struct suit_storage_driver suit_storage_driver_t
SUIT storage backend driver struct.
suit_storage_has_location
static bool suit_storage_has_location(suit_storage_t *storage, const char *location)
Check if this storage backend services a location.
Definition: storage.h:527
suit_storage_init_all
void suit_storage_init_all(void)
initialize all storage backends
suit_storage_init
static int suit_storage_init(suit_storage_t *storage)
One-time initialization function.
Definition: storage.h:398
suit_storage_set_active_location
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:570
suit_manifest_t
SUIT manifest struct.
Definition: suit.h:233
suit_storage_get_separator
static char suit_storage_get_separator(const suit_storage_t *storage)
get the separator for a storage backend
Definition: storage.h:360
suit_storage_match_offset
static int 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:546