Skip to content
Open
12 changes: 12 additions & 0 deletions src/BootControlBlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
#define TYPICAL_NAND_READ_SIZE 2048

#define BCB_MAGIC_OFFSET 12
#define FCB_OFFSET 2 // I do not know the reason for the two bytes.

#define BCB_READ_VIA_FILE_API (1 << 0)
#define BCB_READ_NCB (1 << 1)
#define BCB_READ_LDLB (1 << 2)
#define BCB_READ_DBBT (1 << 3)
#define BCB_READ_DBBT_FROM_FCB (1 << 4)
#define BCB_READ_FCB (1 << 5)

#define MAXSEQLEN 183

Expand Down Expand Up @@ -340,6 +348,10 @@ typedef struct {
union {
struct fcb_block FCB_Block;
union {
struct {
uint32_t m_u32res;
uint32_t m_u32DBBTNumOfPages;
};
struct {
uint32_t m_u32NumberBB; //!< # Bad Blocks stored in this table for NAND0.
uint32_t m_u32Number2KPagesBB; //!< Bad Blocks for NAND0 consume this # of 2K pages.
Expand Down
77 changes: 49 additions & 28 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void usage(void)
" -x .................................... Add 1k-padding in the head\n"
" -n .................................... Dry run (don't commit to flash)\n"
" -w .................................... Commit to flash\n"
" -s .................................... Switch Firmware_startingPages 1<->2\n"
"\n"
" update [-v] [KEY] [KOBS] [-0|1] <file> .. Update a single bootstream\n"
" -v .................................... Verbose mode\n"
Expand Down Expand Up @@ -179,7 +180,7 @@ int extract_main(int argc, char **argv)
char buf[512];
struct mtd_config cfg;
uint8_t key[16];
long end_of_file, pos;
long end_of_file = 0, pos;
char ascii[20 * 2 + 1];
FILE *tfp;
int readn, chunk, curr;
Expand Down Expand Up @@ -263,20 +264,35 @@ int extract_main(int argc, char **argv)
}

chip = 0;
startpage = image == 0 ?
md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector :
md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector2;
size = image == 0 ?
md->curr_ldlb->LDLB_Block2.m_uSectorsInFirmware :
md->curr_ldlb->LDLB_Block2.m_uSectorsInFirmware2 ;
if (plat_config_data->m_u32BCBBlocksFlags & BCB_READ_DBBT_FROM_FCB) {
if (image != 0) {
fprintf(stderr, "Multichip NAND behavior not supported.\n");
exit(5);
}
startpage = md->fcb.FCB_Block.m_u32Firmware1_startingPage;
size = md->fcb.FCB_Block.m_u32PagesInFirmware1;
} else {
startpage = image == 0 ?
md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector :
md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector2;
size = image == 0 ?
md->curr_ldlb->LDLB_Block2.m_uSectorsInFirmware :
md->curr_ldlb->LDLB_Block2.m_uSectorsInFirmware2;
}
if (md->flags & F_MULTICHIP)
chip = image;

if (flags & F_VERBOSE)
printf("startpage=%u, size=%u\n", startpage, size);

start = startpage * 2048;
size = size * 2048;

if (plat_config_data->m_u32BCBBlocksFlags & BCB_READ_FCB) {
start = startpage * md->fcb.FCB_Block.m_u32PageDataSize;
size = size * md->fcb.FCB_Block.m_u32PageDataSize;
} else {
start = startpage * 2048;
size = size * 2048;
}
while (size > 0) {

/* skip bad blocks */
Expand All @@ -292,7 +308,7 @@ int extract_main(int argc, char **argv)
exit(5);
}
r = fwrite(buf, sizeof(buf), 1, outfp);
if (sizeof(buf) != 1)
if (r != 1)
fprintf(stderr, "Write to file failed\n");

start += sizeof(buf);
Expand Down Expand Up @@ -410,26 +426,28 @@ static int perform_bootstream_update(struct mtd_data *md, FILE *infp, int image_
if ((image_mask & (1 << i)) == 0)
continue;

/* first verify it fits */
if (i == 0) {
start = md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector * 2048;
end = md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector2 * 2048;
} else {
start = md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector2 * 2048;
end = mtd_size(md);
}
avail = end - start;
if (plat_config_data->m_u32BCBBlocksFlags & BCB_READ_LDLB) {
/* first verify it fits */
if (i == 0) {
start = md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector * 2048;
end = md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector2 * 2048;
} else {
start = md->curr_ldlb->LDLB_Block2.m_u32Firmware_startingSector2 * 2048;
end = mtd_size(md);
}
avail = end - start;

if (avail <= size) {
fprintf(stderr, "image #d does not fit (avail = %u, size = %u)\n", avail, size);
exit(5);
}
if (avail <= size) {
fprintf(stderr, "image #d does not fit (avail = %u, size = %u)\n", avail, size);
exit(5);
}

/* now update size */
if (i == 0)
md->curr_ldlb->LDLB_Block2.m_uSectorsInFirmware = (size + 2047) / 2048;
else
md->curr_ldlb->LDLB_Block2.m_uSectorsInFirmware2 = (size + 2047) / 2048;
/* now update size */
if (i == 0)
md->curr_ldlb->LDLB_Block2.m_uSectorsInFirmware = (size + 2047) / 2048;
else
md->curr_ldlb->LDLB_Block2.m_uSectorsInFirmware2 = (size + 2047) / 2048;
}
update |= UPDATE_BS(i);
}

Expand Down Expand Up @@ -659,6 +677,9 @@ int init_main(int argc, char **argv)
case 'v':
flags |= F_VERBOSE;
break;
case 's':
flags |= F_FW_SLOT_SWITCH;
break;
}
}

Expand Down
Loading