futility/cmd_vbutil_kernel.c
/* This reads a complete kernel partition into a buffer */
This reads a complete kernel partition into a buffer
This reads a complete kernel partition into a buffer
static uint8_t *ReadOldKPartFromFileOrDie(const char *filename,
					 uint32_t *size_ptr)
{
	FILE *fp = NULL;
	struct stat statbuf;
	uint8_t *buf;
	uint32_t file_size = 0;
	if (0 != stat(filename, &statbuf))
		FATAL("Unable to stat %s: %s\n", filename, strerror(errno));
	if (S_ISBLK(statbuf.st_mode)) {
#if !defined(HAVE_MACOS) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
		int fd = open(filename, O_RDONLY);
		if (fd >= 0) {
			ioctl(fd, BLKGETSIZE64, &file_size);
			close(fd);
		}
#endif
	} else {
		file_size = statbuf.st_size;
	}
	VB2_DEBUG("%s size is %#x\n", filename, file_size);
	if (file_size < opt_pad)
		FATAL("%s is too small to be a valid kernel blob\n", filename);
	VB2_DEBUG("Reading %s\n", filename);
	fp = fopen(filename, "rb");
	if (!fp)
		FATAL("Unable to open file %s: %s\n", filename,
		      strerror(errno));
	buf = malloc(file_size);
	if (1 != fread(buf, file_size, 1, fp))
		FATAL("Unable to read entirety of %s: %s\n", filename,
		      error_fread(fp));
	if (size_ptr)
		*size_ptr = file_size;
	fclose(fp);
	return buf;
}
static uint8_t * ReadOldKPartFromFileOrDie ( const char * filename , uint32_t * size_ptr ) { FILE * fp = NULL ; struct stat statbuf ; uint8_t * buf ; uint32_t file_size = 0 ; if ( 0 != stat ( filename , & statbuf ) ) FATAL ( " \n " , filename , strerror ( errno ) ) ; if ( S_ISBLK ( statbuf . st_mode ) ) { #if ! defined ( HAVE_MACOS ) && ! defined ( __FreeBSD__ ) && ! defined ( __OpenBSD__ ) int fd = open ( filename , O_RDONLY ) ; if ( fd >= 0 ) { ioctl ( fd , BLKGETSIZE64 , & file_size ) ; close ( fd ) ; } #endif } else { file_size = statbuf . st_size ; } VB2_DEBUG ( " \n " , filename , file_size ) ; if ( file_size < opt_pad ) FATAL ( " \n " , filename ) ; VB2_DEBUG ( " \n " , filename ) ; fp = fopen ( filename , " " ) ; if ( ! fp ) FATAL ( " \n " , filename , strerror ( errno ) ) ; buf = malloc ( file_size ) ; if ( 1 != fread ( buf , file_size , 1 , fp ) ) FATAL ( " \n " , filename , error_fread ( fp ) ) ; if ( size_ptr ) * size_ptr = file_size ; fclose ( fp ) ; return buf ; }
This reads a complete kernel partition into a buffer
This reads a complete kernel partition into a buffer