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

Function map_file

util/smmstoretool/utils.c:68–105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

66}
67
68struct mem_range_t map_file(const char path[], bool rw)
69{
70 struct mem_range_t range = {0};
71
72 int open_flags = rw ? O_RDWR : O_RDONLY;
73 int mmap_flags = rw ? PROT_READ | PROT_WRITE : PROT_READ;
74
75 int fd = open(path, open_flags);
76 if (fd == -1) {
77 fprintf(stderr, "Failed to open(): %s\n", strerror(errno));
78 return range;
79 }
80
81 struct stat stat_buf;
82 if (fstat(fd, &stat_buf) != 0) {
83 (void)close(fd);
84 fprintf(stderr, "Failed to fstat(): %s\n", strerror(errno));
85 return range;
86 }
87
88 if (stat_buf.st_size == 0) {
89 (void)close(fd);
90 fprintf(stderr, "Can't map an empty \"%s\" file\n", path);
91 return range;
92 }
93
94 uint8_t *mem = mmap(/*addr=*/NULL, stat_buf.st_size, mmap_flags,
95 MAP_SHARED | MAP_POPULATE, fd, /*offset=*/0);
96 (void)close(fd);
97 if (mem == MAP_FAILED) {
98 fprintf(stderr, "Failed to mmap(): %s\n", strerror(errno));
99 return range;
100 }
101
102 range.start = mem;
103 range.length = stat_buf.st_size;
104 return range;
105}
106
107void unmap_file(struct mem_range_t store)
108{

Callers 2

storage_openFunction · 0.70
make_dataFunction · 0.70

Calls 2

fprintfFunction · 0.85
strerrorFunction · 0.85

Tested by

no test coverage detected