#ifndef G474_BOOT_WRITER_H #define G474_BOOT_WRITER_H #include #include #include /* ECC Flash must be programmed once per 64-bit word. Keep partial words in RAM * across SET packets; pad only the final word, without changing the image CRC. */ typedef bool (*g474_program_word)(void *user, uint32_t offset, uint64_t word); typedef struct { uint32_t size, received; uint8_t pending[8]; uint8_t used; bool failed; } g474_writer; static inline void g474_writer_init(g474_writer *w, uint32_t size) { memset(w, 0, sizeof(*w)); w->size = size; memset(w->pending, 0xff, sizeof(w->pending)); } static inline bool g474_writer_write(g474_writer *w, uint32_t offset, const uint8_t *data, uint16_t length, g474_program_word program, void *user) { if (w->failed || !length || offset != w->received || offset > w->size || length > w->size - offset) return false; while (length--) { w->pending[w->used++] = *data++; ++w->received; if (w->used == 8 || w->received == w->size) { uint64_t word; uint32_t start = w->received - w->used; memcpy(&word, w->pending, sizeof(word)); if (!program(user, start, word)) { w->failed = true; return false; } w->used = 0; memset(w->pending, 0xff, sizeof(w->pending)); } } return true; } #endif