MCPcopy Create free account
hub / github.com/coreboot/coreboot / read_file

Function read_file

util/intelvbttool/intelvbttool.c:370–414  ·  view source on GitHub ↗

Create fileobject from file */

Source from the content-addressed store, hash-verified

368
369/* Create fileobject from file */
370static struct fileobject *read_file(const char *filename)
371{
372 FILE *fd = fopen(filename, "rb");
373 off_t read_size = DEF_ALLOC;
374
375 if (!fd) {
376 printerr("%s open failed: %s\n", filename, strerror(errno));
377 return NULL;
378 }
379
380 struct fileobject *fo = malloc_fo(read_size);
381 if (!fo) {
382 printerr("malloc failed\n");
383 fclose(fd);
384 return NULL;
385 }
386
387 off_t total_bytes_read = 0, bytes_read;
388 while ((bytes_read = fread(fo->data + total_bytes_read, 1, read_size, fd)) > 0) {
389 total_bytes_read += bytes_read;
390 struct fileobject *newfo = remalloc_fo(fo, fo->size + read_size);
391 if (!newfo) {
392 fclose(fd);
393 free_fo(fo);
394 return NULL;
395 }
396 fo = newfo;
397 }
398
399 if (!total_bytes_read) {
400 fclose(fd);
401 free_fo(fo);
402 return NULL;
403 }
404
405 if (fclose(fd)) {
406 printerr("%s close failed: %s\n", filename, strerror(errno));
407 free_fo(fo);
408 return NULL;
409 }
410
411 fo->size = total_bytes_read;
412
413 return fo;
414}
415
416/* Create fileobject from physical memory at given address of size 64 KiB */
417static struct fileobject *read_physmem(size_t addr)

Callers 1

mainFunction · 0.70

Calls 8

fopenFunction · 0.85
printerrFunction · 0.85
strerrorFunction · 0.85
malloc_foFunction · 0.85
fcloseFunction · 0.85
freadFunction · 0.85
remalloc_foFunction · 0.85
free_foFunction · 0.85

Tested by

no test coverage detected