Codex 69521e54e0 Add 15 embedded repair tasks and correct grader-read controls
Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
2026-10-16 14:23:48 +08:00

23 lines
12 KiB
JSON

{
"kind": "flash",
"seed": 161023,
"context": {
"update.h": "#ifndef UPDATE_H\n#define UPDATE_H\n#include <stddef.h>\n#include <stdint.h>\n#define FLASH_SIZE 2048u\n#define FLASH_PAGE 64u\n#define FLASH_PROGRAM_MAX 32u\n#define FLASH_PROTECTED_LOW 128u\n#define FLASH_PROTECTED_HIGH 1920u\n#define UPDATE_HEADER 128u\n#define UPDATE_DATA 256u\n#define UPDATE_MAX 512u\n#define UPDATE_CHUNK 32u\n#define UPDATE_CELL 64u\n\ntypedef struct {\n int powered;\n int remaining;\n uint64_t operations;\n} Flash;\n\ntypedef struct {\n Flash *flash;\n unsigned open;\n uint32_t total;\n uint32_t expected_crc;\n uint32_t next;\n unsigned finished;\n} Update;\n\nuint32_t util_u32(const uint8_t *);\nvoid util_put32(uint8_t *, uint32_t);\nuint32_t util_crc32(const uint8_t *, size_t);\nint util_read_bytes(uint8_t *, size_t);\nvoid util_print_bytes(const uint8_t *, size_t);\nint flash_create(Flash *);\nvoid flash_poweron(Flash *);\nvoid flash_arm(Flash *, int);\nint flash_read(Flash *, uint32_t, uint8_t *, size_t);\nint flash_program(Flash *, uint32_t, const uint8_t *, size_t);\nint flash_erase(Flash *, uint32_t, size_t);\nint flash_inspect(uint32_t, uint8_t *, size_t);\nint flash_corrupt(uint32_t, uint8_t);\nvoid update_init(Update *, Flash *);\nint update_begin(Update *, uint32_t, uint32_t);\nint update_write(Update *, uint32_t, const uint8_t *, size_t);\nint update_finish(Update *);\nint update_recover(Update *);\nint journal_header(Update *);\nint journal_append(Update *, uint32_t, const uint8_t *, size_t);\nint journal_payload(Update *, uint8_t *);\n#endif\n",
"util.c": "#include \"update.h\"\n#include <stdio.h>\n\nuint32_t util_u32(const uint8_t *bytes)\n{\n uint32_t value = 0;\n for (unsigned i = 0; i < 4; ++i) {\n value |= (uint32_t)bytes[i] << (i * 8);\n }\n return value;\n}\n\nvoid util_put32(uint8_t *bytes, uint32_t value)\n{\n for (unsigned i = 0; i < 4; ++i) {\n bytes[i] = (uint8_t)(value >> (i * 8));\n }\n}\n\nuint32_t util_crc32(const uint8_t *bytes, size_t count)\n{\n uint32_t crc = 0xffffffffu;\n for (size_t i = 0; i < count; ++i) {\n crc ^= bytes[i];\n for (unsigned bit = 0; bit < 8; ++bit) {\n uint32_t mask = 0u - (crc & 1u);\n crc = (crc >> 1) ^ (0xedb88320u & mask);\n }\n }\n return crc ^ 0xffffffffu;\n}\n\nint util_read_bytes(uint8_t *bytes, size_t count)\n{\n for (size_t i = 0; i < count; ++i) {\n unsigned value;\n if (scanf(\"%u\", &value) != 1 || value > 255) {\n return 0;\n }\n bytes[i] = (uint8_t)value;\n }\n return 1;\n}\n\nvoid util_print_bytes(const uint8_t *bytes, size_t count)\n{\n if (count == 0) {\n putchar('-');\n }\n for (size_t i = 0; i < count; ++i) {\n printf(\"%02x\", bytes[i]);\n }\n}\n",
"flash.c": "#include \"update.h\"\n#include <stdio.h>\n#include <string.h>\n#define FLASH_FILE \"flash.bin\"\n\nstatic int file_read(uint32_t offset, uint8_t *bytes, size_t count)\n{\n if (offset > FLASH_SIZE || count > FLASH_SIZE - offset) {\n return -3;\n }\n FILE *file = fopen(FLASH_FILE, \"rb\");\n if (file == NULL) {\n return -3;\n }\n int result = 0;\n if (fseek(file, (long)offset, SEEK_SET) != 0 ||\n fread(bytes, 1, count, file) != count) {\n result = -3;\n }\n if (fclose(file) != 0) {\n result = -3;\n }\n return result;\n}\n\nstatic int file_write(uint32_t offset, const uint8_t *bytes, size_t count)\n{\n FILE *file = fopen(FLASH_FILE, \"r+b\");\n if (file == NULL) {\n return -3;\n }\n int result = 0;\n if (fseek(file, (long)offset, SEEK_SET) != 0 ||\n fwrite(bytes, 1, count, file) != count || fflush(file) != 0) {\n result = -3;\n }\n if (fclose(file) != 0) {\n result = -3;\n }\n return result;\n}\n\nint flash_create(Flash *flash)\n{\n uint8_t bytes[FLASH_SIZE];\n memset(bytes, 0xff, sizeof(bytes));\n for (unsigned i = 0; i < FLASH_SIZE; ++i) {\n if (i < FLASH_PROTECTED_LOW || i >= FLASH_PROTECTED_HIGH) {\n bytes[i] = (uint8_t)(i * 13u + 17u);\n }\n }\n FILE *file = fopen(FLASH_FILE, \"wb\");\n if (file == NULL) {\n return -3;\n }\n int ok = fwrite(bytes, 1, sizeof(bytes), file) == sizeof(bytes);\n if (fclose(file) != 0) {\n ok = 0;\n }\n flash->powered = 1;\n flash->remaining = -1;\n flash->operations = 0;\n return ok ? 0 : -3;\n}\n\nvoid flash_poweron(Flash *flash)\n{\n flash->powered = 1;\n flash->remaining = -1;\n}\n\nvoid flash_arm(Flash *flash, int remaining)\n{\n flash->remaining = remaining;\n}\n\nstatic int mutation(Flash *flash)\n{\n if (!flash->powered) {\n return -9;\n }\n if (flash->remaining == 0) {\n flash->powered = 0;\n return -9;\n }\n if (flash->remaining > 0) {\n --flash->remaining;\n }\n ++flash->operations;\n return 0;\n}\n\nstatic int writable(uint32_t offset, size_t count)\n{\n return offset >= FLASH_PROTECTED_LOW &&\n offset <= FLASH_PROTECTED_HIGH &&\n count <= FLASH_PROTECTED_HIGH - offset;\n}\n\nint flash_read(Flash *flash, uint32_t offset, uint8_t *bytes, size_t count)\n{\n if (!flash->powered) {\n return -9;\n }\n return file_read(offset, bytes, count);\n}\n\nint flash_program(Flash *flash, uint32_t offset, const uint8_t *bytes,\n size_t count)\n{\n if (count == 0 || count > FLASH_PROGRAM_MAX ||\n !writable(offset, count) ||\n offset / FLASH_PAGE != (offset + count - 1) / FLASH_PAGE) {\n return -3;\n }\n uint8_t old[FLASH_PROGRAM_MAX];\n int result = flash_read(flash, offset, old, count);\n if (result != 0) {\n return result;\n }\n for (size_t i = 0; i < count; ++i) {\n if ((old[i] & bytes[i]) != bytes[i]) {\n return -3;\n }\n }\n result = mutation(flash);\n if (result != 0) {\n return result;\n }\n return file_write(offset, bytes, count);\n}\n\nint flash_erase(Flash *flash, uint32_t offset, size_t count)\n{\n if (count != FLASH_PAGE || offset % FLASH_PAGE != 0 ||\n !writable(offset, count)) {\n return -3;\n }\n int result = mutation(flash);\n if (result != 0) {\n return result;\n }\n uint8_t erased[FLASH_PAGE];\n memset(erased, 0xff, sizeof(erased));\n return file_write(offset, erased, sizeof(erased));\n}\n\nint flash_inspect(uint32_t offset, uint8_t *bytes, size_t count)\n{\n return file_read(offset, bytes, count);\n}\n\nint flash_corrupt(uint32_t offset, uint8_t value)\n{\n if (offset >= FLASH_SIZE) {\n return -3;\n }\n return file_write(offset, &value, 1);\n}\n",
"main.c": "#include \"update.h\"\n#include <stdio.h>\n#include <string.h>\n#include <inttypes.h>\n\nstatic void snapshot(const Update *u, char command, int result)\n{\n printf(\"%c %d %u %\" PRIu32 \" %\" PRIu32 \" %u %d\\n\", command,\n result, u->open, u->total, u->next, u->finished, u->flash->powered);\n}\n\nstatic int write_command(Update *update)\n{\n uint32_t offset;\n unsigned count;\n uint8_t bytes[512];\n if (scanf(\"%\" SCNu32 \" %u\", &offset, &count) != 2 ||\n count > sizeof(bytes) || !util_read_bytes(bytes, count)) {\n return 0;\n }\n int result = update_write(update, offset, bytes, count);\n printf(\"N %\" PRIu32 \"\\n\", util_crc32(bytes, count));\n snapshot(update, 'W', result);\n return 1;\n}\n\nstatic int dump_command(void)\n{\n uint32_t offset;\n unsigned count;\n uint8_t bytes[FLASH_SIZE];\n if (scanf(\"%\" SCNu32 \" %u\", &offset, &count) != 2 || count > sizeof(bytes)) {\n return 0;\n }\n int result = flash_inspect(offset, bytes, count);\n printf(\"D %d\", result);\n if (result == 0) {\n putchar(' ');\n util_print_bytes(bytes, count);\n }\n putchar('\\n');\n return 1;\n}\n\nstatic int raw_program(Update *update)\n{\n uint32_t offset;\n unsigned count;\n uint8_t bytes[512];\n if (scanf(\"%\" SCNu32 \" %u\", &offset, &count) != 2 ||\n count > sizeof(bytes) || !util_read_bytes(bytes, count)) {\n return 0;\n }\n int result = flash_program(update->flash, offset, bytes, count);\n snapshot(update, 'P', result);\n return 1;\n}\n\nint main(void)\n{\n Flash flash;\n Update update;\n char command[8];\n if (flash_create(&flash) != 0) {\n return 3;\n }\n update_init(&update, &flash);\n while (scanf(\"%7s\", command) == 1) {\n int ok = 1;\n if (strcmp(command, \"I\") == 0) {\n int result = flash_create(&flash);\n update_init(&update, &flash);\n snapshot(&update, 'I', result);\n } else if (strcmp(command, \"B\") == 0) {\n uint32_t total, checksum;\n if (scanf(\"%\" SCNu32 \" %\" SCNu32, &total, &checksum) != 2) {\n return 2;\n }\n int result = update_begin(&update, total, checksum);\n snapshot(&update, 'B', result);\n } else if (strcmp(command, \"W\") == 0) {\n ok = write_command(&update);\n } else if (strcmp(command, \"F\") == 0) {\n int result = update_finish(&update);\n snapshot(&update, 'F', result);\n } else if (strcmp(command, \"A\") == 0) {\n int count;\n if (scanf(\"%d\", &count) != 1 || count < -1) {\n return 2;\n }\n flash_arm(&flash, count);\n snapshot(&update, 'A', 0);\n } else if (strcmp(command, \"R\") == 0) {\n flash_poweron(&flash);\n int result = update_recover(&update);\n snapshot(&update, 'R', result);\n } else if (strcmp(command, \"K\") == 0) {\n uint32_t offset;\n unsigned value;\n if (scanf(\"%\" SCNu32 \" %u\", &offset, &value) != 2 || value > 255) {\n return 2;\n }\n int result = flash_corrupt(offset, (uint8_t)value);\n snapshot(&update, 'K', result);\n } else if (strcmp(command, \"G\") == 0) {\n uint8_t bytes[UPDATE_MAX];\n int result = journal_payload(&update, bytes);\n printf(\"G %d\", result);\n if (result == 0) {\n putchar(' ');\n util_print_bytes(bytes, update.total);\n }\n putchar('\\n');\n } else if (strcmp(command, \"J\") == 0) {\n uint32_t offset;\n unsigned count;\n uint8_t bytes[FLASH_SIZE];\n if (scanf(\"%\" SCNu32 \" %u\", &offset, &count) != 2 ||\n count > sizeof(bytes)) {\n return 2;\n }\n int result = flash_inspect(offset, bytes, count);\n printf(\"J %d\", result);\n if (result == 0) {\n printf(\" %\" PRIu32, util_crc32(bytes, count));\n }\n putchar('\\n');\n } else if (strcmp(command, \"D\") == 0) {\n ok = dump_command();\n } else if (strcmp(command, \"P\") == 0) {\n ok = raw_program(&update);\n } else if (strcmp(command, \"E\") == 0) {\n uint32_t offset;\n unsigned count;\n if (scanf(\"%\" SCNu32 \" %u\", &offset, &count) != 2) {\n return 2;\n }\n int result = flash_erase(&flash, offset, count);\n snapshot(&update, 'E', result);\n } else if (strcmp(command, \"S\") == 0) {\n snapshot(&update, 'S', 0);\n } else {\n ok = 0;\n }\n if (!ok) {\n fprintf(stderr, \"invalid command\\n\");\n return 2;\n }\n }\n return 0;\n}\n",
"Makefile": "CC = gcc\nCFLAGS = -std=c11 -O2 -Wall -Wextra -Werror -pedantic\nSOURCES = main.c util.c flash.c update.c journal.c\nall: observe\nobserve: $(SOURCES) update.h\n\t$(CC) $(CFLAGS) $(SOURCES) -o observe\nclean:\n\trm -f observe flash.bin\n.PHONY: all clean\n"
},
"sources": [
"main.c",
"util.c",
"flash.c",
"update.c",
"journal.c"
],
"candidates": [
"update.c",
"journal.c"
]
}