Skip to content
Snippets Groups Projects
Commit 6c2457cc authored by Ronny Nilsson's avatar Ronny Nilsson
Browse files

- Support for wrapped metadata sequence number.

- Some cleaning.
parent 40d54a94
Branches
No related tags found
No related merge requests found
......@@ -27,6 +27,10 @@
#include "brcm_fw_tool.h"
//-------------------------------------------------------------
#define MAX_META_SEQ 999 // The maximum metadata sequence number.
/*
* Broadcom custom binary of UBI volumes METADATA and filestruct_full.bin
* # This is a sequence of records which may be aligned with padding, each contains the following in this order:
......@@ -488,7 +492,8 @@ static int meta_find_seqno(struct metaInfo_t *metaInfo)
0, ubi_read_blk, NULL, NULL, NULL, inFd) == 3) {
outBuf[3] = 0;
metaInfo->seqNo = strtol(outBuf, NULL, 10);
if(metaInfo->seqNo < 0 || metaInfo->seqNo > 999 || errno == ERANGE) {
if(metaInfo->seqNo < 0 || metaInfo->seqNo > MAX_META_SEQ ||
errno == ERANGE) {
if (verbose) printf("Invalid sequence value!\n");
res = -1;
}
......@@ -662,20 +667,19 @@ static int meta_generate_record(int fd, const char *name, const void *data, int
//-------------------------------------------------------------
// Generate a Broadcom metadata UBI volume blob having
// the sequnce number <seqNo>. Return non-zero on error.
static int meta_generate_blob(int seqNo)
static int meta_generate_blob(const char *tmpPath, int seqNo)
{
const uint32_t zero = 0;
int outFd, res;
char data[16];
res = 0;
if(seqNo < 0 || seqNo > 999) return -1;
if(seqNo < 0 || seqNo > MAX_META_SEQ || !tmpPath) return -1;
// TODO: don't write a file! Use a small memory buffer instead!
outFd = open("/tmp/ronny", O_WRONLY | O_CREAT | O_EXCL | O_TRUNC |
outFd = open(tmpPath, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC |
O_NOFOLLOW, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(outFd == -1) {
perror("Error creating file");
fprintf(stderr, "Error creating file %s: %s", tmpPath, strerror(errno));
return -1;
}
......@@ -704,12 +708,14 @@ static int meta_generate_blob(int seqNo)
// of time we also need a third for ensuring atomicity.
static int meta_manage_vols(void)
{
int highSeqNo, highMeta, i, newMeta, delMeta;
int highSeqNo, highMeta, i, newMeta, delMeta, wrapSeqNo;
struct metaInfo_t *tmpMeta;
highSeqNo = 0;
highMeta = 0;
wrapSeqNo = -1;
// What is the highest metadata sequence number?
for(i = 0; i < MAX_METAS; i++) {
if(metaInfos[i].found && metaInfos[i].committed &&
metaInfos[i].seqNo >= 0 && metaInfos[i].seqNo > highSeqNo) {
......@@ -718,6 +724,20 @@ static int meta_manage_vols(void)
}
}
// Check for sequence number wrap around and compensate.
if(highSeqNo > MAX_META_SEQ - MAX_METAS) {
for(i = 0; i < MAX_METAS; i++) {
if(metaInfos[i].found && metaInfos[i].committed &&
metaInfos[i].seqNo >= 0 &&
metaInfos[i].seqNo < MAX_METAS &&
metaInfos[i].seqNo > wrapSeqNo) {
wrapSeqNo = metaInfos[i].seqNo;
highSeqNo = metaInfos[i].seqNo;
highMeta = i;
}
}
}
// Decide which old meta volume to erase and which to create fresh.
switch(metaInfos[highMeta].volInfo.vol_id) {
case METAVOLID0:
......@@ -759,6 +779,12 @@ static int meta_manage_vols(void)
printf(".\n");
}
// Calc new (wrapping) sequence number and create a metadata binary.
if(meta_generate_blob("/tmp/ronny",
(highSeqNo + 1) % (MAX_META_SEQ + 1))) {
return -1;
}
tmpMeta = meta_by_id(newMeta);
if(tmpMeta) ubi_erase_volume(newMeta);
if(ubi_create_volume(newMeta, devInfo.leb_size)) return -1;
......@@ -775,7 +801,7 @@ static int meta_manage_vols(void)
// main() of Broadcom "METADATA" UBI volume management.
int gen_image_ubi(const char *in_file, char *sequence_number)
{
int res, seqNo, i;
int res, i;
// Init
res = 0;
......@@ -796,8 +822,6 @@ int gen_image_ubi(const char *in_file, char *sequence_number)
if(!res) res = ubi_probe();
if(!res) res = meta_find_all();
meta_generate_blob((seqNo + 1) % 1000);
if(!res) res = meta_manage_vols();
unlink("/tmp/ronny"); // Possibly erase temporary file.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment