Skip to content
Snippets Groups Projects
ifdtool.c 28 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	filename = argv[optind];
    	if (optind + 2 != argc)
    		outfile = argv[optind + 1];
    
    	if (create)
    		bios_fd = open(filename, O_WRONLY | O_CREAT, 0666);
    	else
    		bios_fd = open(filename, outfile ? O_RDONLY : O_RDWR);
    
    	if (bios_fd == -1) {
    		perror("Could not open file");
    		exit(EXIT_FAILURE);
    	}
    
    	if (!create) {
    		if (fstat(bios_fd, &buf) == -1) {
    			perror("Could not stat file");
    			exit(EXIT_FAILURE);
    		}
    		size = buf.st_size;
    	}
    
    	debug("File %s is %d bytes\n", filename, size);
    
    	if (rom_size == -1)
    		rom_size = size;
    
    	image = malloc(rom_size);
    	if (!image) {
    		printf("Out of memory.\n");
    		exit(EXIT_FAILURE);
    	}
    
    	memset(image, '\xff', rom_size);
    	if (!create && read(bios_fd, image, size) != size) {
    		perror("Could not read file");
    		exit(EXIT_FAILURE);
    	}
    	if (size != rom_size) {
    		debug("ROM size changed to %d bytes\n", rom_size);
    		size = rom_size;
    	}
    
    	write_it = true;
    	ret = 0;
    	if (mode_dump) {
    		ret = dump_fd(image, size);
    		write_it = false;
    	}
    
    	if (mode_extract) {
    		ret = write_regions(image, size);
    		write_it = false;
    	}
    
    	if (mode_write_descriptor)
    
    		ret = write_data(image, size, -size, desc_fname, 0, 0);
    
    		ret = inject_region(image, size, region_type, inject_fname);
    
    		int offset_uboot_start = 0;
    
    		for (wr_idx = 0; wr_idx < wr_num; wr_idx++) {
    
    			ifile = &input_file[wr_idx];
    
    			ret = write_data(image, size, ifile->addr,
    					 ifile->fname, offset_uboot_top,
    					 offset_uboot_start);
    
    
    	if (mode_spifreq)
    		set_spi_frequency(image, size, spifreq);
    
    	if (mode_em100)
    		set_em100_mode(image, size);
    
    	if (mode_locked)
    		lock_descriptor(image, size);
    
    	if (mode_unlocked)
    		unlock_descriptor(image, size);
    
    	if (write_it) {
    		if (outfile) {
    			ret = write_image(outfile, image, size);
    		} else {
    			if (lseek(bios_fd, 0, SEEK_SET)) {
    				perror("Error while seeking");
    				ret = -1;
    			}
    			if (write(bios_fd, image, size) != size) {
    				perror("Error while writing");
    				ret = -1;
    			}
    		}
    	}
    
    	free(image);
    	close(bios_fd);
    
    
    	return ret < 0 ? 1 : 0;