Skip to content
Snippets Groups Projects
dumpimage.c 1.75 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jonas Höglund's avatar
    Jonas Höglund committed
    /*
     * dumpimage -- Tool to extract sub images from FIT image.
     *
     * Copyright (C) 2021 IOPSYS Software Solutions AB. All rights reserved.
     *
     * Author: jonas.hoglund@iopsys.eu
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     * This program is distributed in the hope that it will be useful, but
     * WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
     * 02110-1301 USA
     */
    
    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    
    #include <stdarg.h>
    
    Jonas Höglund's avatar
    Jonas Höglund committed
    #include <string.h>
    #include <stdbool.h>
    
    #include <sys/stat.h>
    
    Jonas Höglund's avatar
    Jonas Höglund committed
    
    #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);
    }
    
    
    Jonas Höglund's avatar
    Jonas Höglund committed
    int main(int argc, char *argv[])
    {
    
    	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");
    
    Jonas Höglund's avatar
    Jonas Höglund committed
    
    
    	free(buf);
    	fclose(f);
    
    Jonas Höglund's avatar
    Jonas Höglund committed
    }