tools: add tool to create empty flash image

tags/0.1.0
S.J.R. van Schaik 8 years ago
parent 80a5ecc64c
commit eca2bc7b17
  1. 2
      Makefile
  2. 7
      scripts/Makefile.host
  3. 69
      source/tools/create_image.c

@ -36,6 +36,8 @@ MAKE := make
.SECONDARY:
all: tools
clean:
@echo "CLEAN"
@rm -rf $(BUILD)

@ -1,2 +1,9 @@
obj-y += source/drivers/sandbox_flash.o
obj-y += source/drivers/stdio_console.o
tools: $(BUILD)/create-image
$(BUILD)/create-image: $(BUILD)/source/tools/create_image.o
@echo "LD $@"
@mkdir -p $(dir $@)
@$(LD) -o $@ $(CFLAGS) $(LDFLAGS) $^

@ -0,0 +1,69 @@
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define min(x, y) ((x < y) ? (x) : (y))
static int parse_size(size_t *size, const char *s)
{
char *end;
if (!size || !s)
return -1;
*size = strtoul(s, &end, 10);
switch (toupper(*end)) {
case 'G': *size *= 1024;
case 'M': *size *= 1024;
case 'K': *size *= 1024;
case 'B': ++end; break;
case '\0': return 0;
default: return -1;
}
if (*end)
return -1;
return 0;
}
int main(int argc, char *argv[])
{
uint8_t buf[4096];
FILE *fp;
size_t count, size;
int ret = 0;
if (argc < 3) {
fprintf(stderr, "usage: %s <size> <output>\n", argv[0]);
return -1;
}
if (parse_size(&size, argv[1]) != 0) {
fprintf(stderr, "error: size must be an integer with an optional K, M "
"or G suffix.\n");
return -1;
}
memset(buf, 0xFF, sizeof buf);
if (!(fp = fopen(argv[2], "wb"))) {
fprintf(stderr, "error: unable to open '%s' for writing.\n", argv[2]);
return -1;
}
for (; size; size -= count) {
count = min(sizeof buf, size);
if (fwrite(buf, sizeof *buf, count, fp) < count) {
fprintf(stderr, "error: unable to write to file.\n");
ret = -1;
}
}
fclose(fp);
return ret;
}
Loading…
Cancel
Save