The state framework grew organically over the time. Unfortunately the architecture and abstractions disappeared during this period. This patch refactors the framework to recreate the abstractions. The main focus was the backend with its storage. The main use-case was to offer better NAND support with less erase cycles and interchangeable data formats (dtb,raw). The general architecture now has a backend which consists of a data format and storage. The storage consists of multiple storage buckets each holding exactly one copy of the state data. A data format describes a data serialization for the state framework. This can be either dtb or raw. A storage bucket is a storage location which is used to store any data. There is a (new) circular type which writes changes behind the last written data and therefore reduces the number of erases. The other type is a direct bucket which writes directly to a storage offset for all non-erase storage. Furthermore this patch splits up all classes into different files in a subdirectory. This is currently all in one patch as I can't see a good way to split the changes up without having a non-working state framework in between. The following diagram shows the new architecture roughly: .----------. | state | '----------' | | v .----------------------------. | state_backend | |----------------------------| | + state_load(*state); | | + state_save(*state); | | + state_backend_init(...); | | | | | '----------------------------' | | The format describes | | how the state data | '-------------> is serialized | .--------------------------------------------. | | state_backend_format <INTERFACE> | | |--------------------------------------------| | | + verify(*format, magic, *buf, len); | | | + pack(*format, *state, **buf, len); | | | + unpack(*format, *state, *buf, len); | | | + get_packed_len(*format, *state); | | | + free(*format); | | '--------------------------------------------' | ^ ^ | * * | * * | .--------------------. .--------------------. | | backend_format_dtb | | backend_format_raw | | '--------------------' '--------------------' | | | v .----------------------------------------------------------. | state_backend_storage | |----------------------------------------------------------| | + init(...); | | + free(*storage); | | + read(*storage, *format, magic, **buf, *len, len_hint); | | + write(*storage, *buf, len); | | + restore_consistency(*storage, *buf, len); | '----------------------------------------------------------' | The backend storage is responsible to manage multiple data copies and distribute them onto several buckets. Read data is verified against the given format to ensure that the read data is correct. | | | | | v .------------------------------------------. | state_backend_storage_bucket <INTERFACE> | |------------------------------------------| | + init(*bucket); | | + write(*bucket, *buf, len); | | + read(*bucket, **buf, len_hint); | | + free(*bucket); | '------------------------------------------' ^ ^ ^ * * * * * * A storage bucket represents*exactly one data copy at one data location. A circular b*cket writes any new data to the end of the bucket (for *educed erases on NAND). A direct bucket directly writ*s at one location. * * * * * * * * * .-----------------------. * .-------------------------. | backend_bucket_direct | * | backend_bucket_circular | '-----------------------' * '-------------------------' ^ * ^ | * | | * | | * | | .-----------------------. | '--| backend_bucket_cached |---' '-----------------------' A backend_bucket_cached is a transparent bucket that directly uses another bucket as backend device and caches all accesses. Signed-off-by: Markus Pargmann <mpa@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
151 lines
3.7 KiB
C
151 lines
3.7 KiB
C
/*
|
|
* Copyright (C) 2012-2014 Pengutronix, Jan Luebbe <j.luebbe@pengutronix.de>
|
|
* Copyright (C) 2013-2014 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
|
|
* Copyright (C) 2015 Pengutronix, Marc Kleine-Budde <mkl@pengutronix.de>
|
|
* Copyright (C) 2016 Pengutronix, Markus Pargmann <mpa@pengutronix.de>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* version 2, as published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
*/
|
|
|
|
#include <common.h>
|
|
#include <of.h>
|
|
#include <linux/kernel.h>
|
|
#include <malloc.h>
|
|
|
|
#include "state.h"
|
|
|
|
struct state_backend_format_dtb {
|
|
struct state_backend_format format;
|
|
|
|
struct device_node *root;
|
|
|
|
/* For outputs */
|
|
struct device_d *dev;
|
|
};
|
|
|
|
static inline struct state_backend_format_dtb *get_format_dtb(struct
|
|
state_backend_format
|
|
*format)
|
|
{
|
|
return container_of(format, struct state_backend_format_dtb, format);
|
|
}
|
|
|
|
static int state_backend_format_dtb_verify(struct state_backend_format *format,
|
|
uint32_t magic, const uint8_t * buf,
|
|
ssize_t len)
|
|
{
|
|
struct state_backend_format_dtb *fdtb = get_format_dtb(format);
|
|
struct device_node *root;
|
|
struct fdt_header *fdt = (struct fdt_header *)buf;
|
|
size_t dtb_len = fdt32_to_cpu(fdt->totalsize);
|
|
|
|
if (dtb_len > len) {
|
|
dev_err(fdtb->dev, "Error, stored DTB length (%d) longer than read buffer (%d)\n",
|
|
dtb_len, len);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (fdtb->root) {
|
|
of_delete_node(fdtb->root);
|
|
fdtb->root = NULL;
|
|
}
|
|
|
|
root = of_unflatten_dtb(buf);
|
|
if (IS_ERR(root)) {
|
|
dev_err(fdtb->dev, "Failed to unflatten dtb from buffer with length %zd, %ld\n",
|
|
len, PTR_ERR(root));
|
|
return PTR_ERR(root);
|
|
}
|
|
|
|
fdtb->root = root;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int state_backend_format_dtb_unpack(struct state_backend_format *format,
|
|
struct state *state,
|
|
const uint8_t * buf, ssize_t len)
|
|
{
|
|
struct state_backend_format_dtb *fdtb = get_format_dtb(format);
|
|
int ret;
|
|
|
|
if (!fdtb->root) {
|
|
state_backend_format_dtb_verify(format, 0, buf, len);
|
|
}
|
|
|
|
ret = state_from_node(state, fdtb->root, 0);
|
|
of_delete_node(fdtb->root);
|
|
fdtb->root = NULL;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int state_backend_format_dtb_pack(struct state_backend_format *format,
|
|
struct state *state, uint8_t ** buf,
|
|
ssize_t * len)
|
|
{
|
|
struct state_backend_format_dtb *fdtb = get_format_dtb(format);
|
|
struct device_node *root;
|
|
struct fdt_header *fdt;
|
|
|
|
root = state_to_node(state, NULL, STATE_CONVERT_TO_NODE);
|
|
if (IS_ERR(root)) {
|
|
dev_err(fdtb->dev, "Failed to convert state to device node, %ld\n",
|
|
PTR_ERR(root));
|
|
return PTR_ERR(root);
|
|
}
|
|
|
|
fdt = of_flatten_dtb(root);
|
|
if (!fdt) {
|
|
dev_err(fdtb->dev, "Failed to create flattened dtb\n");
|
|
of_delete_node(root);
|
|
return -EINVAL;
|
|
}
|
|
|
|
*buf = (uint8_t *) fdt;
|
|
*len = fdt32_to_cpu(fdt->totalsize);
|
|
|
|
if (fdtb->root)
|
|
of_delete_node(fdtb->root);
|
|
fdtb->root = root;
|
|
|
|
free(fdt);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void state_backend_format_dtb_free(struct state_backend_format *format)
|
|
{
|
|
struct state_backend_format_dtb *fdtb = get_format_dtb(format);
|
|
|
|
free(fdtb);
|
|
}
|
|
|
|
int backend_format_dtb_create(struct state_backend_format **format,
|
|
struct device_d *dev)
|
|
{
|
|
struct state_backend_format_dtb *dtb;
|
|
|
|
dtb = xzalloc(sizeof(*dtb));
|
|
if (!dtb)
|
|
return -ENOMEM;
|
|
|
|
dtb->dev = dev;
|
|
dtb->format.pack = state_backend_format_dtb_pack;
|
|
dtb->format.unpack = state_backend_format_dtb_unpack;
|
|
dtb->format.verify = state_backend_format_dtb_verify;
|
|
dtb->format.free = state_backend_format_dtb_free;
|
|
dtb->format.name = "dtb";
|
|
*format = &dtb->format;
|
|
|
|
return 0;
|
|
}
|