diff --git a/dumpimage.c b/dumpimage.c index 6fdc4cd32df83c8cf570c9a809ae90c3ea67cbbc..9f42d20d08db9786973c97d95cce32386166f77d 100644 --- a/dumpimage.c +++ b/dumpimage.c @@ -23,18 +23,57 @@ #include <stdio.h> #include <stdint.h> #include <stdlib.h> +#include <stdarg.h> #include <string.h> #include <stdbool.h> +#include <sys/stat.h> #include <libfdt.h> #include <fdt.h> +#define PRINTF(i, j) __attribute__((format (printf, i, j))) +#define NORETURN __attribute__((noreturn)) + +static inline void NORETURN PRINTF(1, 2) die(const char *str, ...) +{ + va_list ap; + + va_start(ap, str); + fprintf(stderr, "ERROR: "); + vfprintf(stderr, str, ap); + va_end(ap); + exit(1); +} + int main(int argc, char *argv[]) { - printf("dumpimage\n"); + const char *file; + FILE *f; + struct stat st; + int size, ret; + char *buf; + + + if (argc < 2) + die("missing filename.\n"); + + file = argv[1]; + + f = fopen(file, "r"); + if (!f) + die("could not open file\n"); + + stat(file, &st); + size = st.st_size; + + buf = malloc(size); + if (!buf) + die("Could not allocate buffer.\n"); + + ret = fread(buf, 1, size, f); + if (ret < size) + die("Could not read file.\n"); - if (argc < 2) { - fprintf(stderr, "Error: missing filename"); - return -1; - } + free(buf); + fclose(f); }