3rdparty/fsp: Update submodule to upstream master
[coreboot.git] / util / smmstoretool / utils.c
blob9f554e050174bfcdbc50fb37d0c81b98109965b9
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include "utils.h"
5 #include <sys/mman.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
10 #include <errno.h>
11 #include <limits.h>
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
18 void *xmalloc(size_t size)
20 void *p = malloc(size);
21 if (p == NULL) {
22 fprintf(stderr, "Failed to allocate memory\n");
23 abort();
25 return p;
28 char *to_chars(const CHAR16 uchars[], size_t size)
30 char *chars = xmalloc(size / 2 + 1);
32 const CHAR16 *from = uchars;
33 char *to = chars;
34 while (*from != 0) {
35 CHAR16 uc = *from++;
36 if (uc < CHAR_MAX)
37 *to++ = uc;
38 else
39 *to++ = '?';
42 // In case there was no terminating NUL.
43 if (to != chars && to[-1] != '\0')
44 *to = '\0';
46 return chars;
49 CHAR16 *to_uchars(const char chars[], size_t *size)
51 *size = (strlen(chars) + 1) * 2;
52 CHAR16 *uchars = xmalloc(*size);
54 const char *from = chars;
55 CHAR16 *to = uchars;
56 while (*from != '\0')
57 *to++ = *from++;
58 *to = 0;
60 return uchars;
63 bool str_eq(const char lhs[], const char rhs[])
65 return strcmp(lhs, rhs) == 0;
68 struct mem_range_t map_file(const char path[], bool rw)
70 struct mem_range_t range = {0};
72 int open_flags = rw ? O_RDWR : O_RDONLY;
73 int mmap_flags = rw ? PROT_READ | PROT_WRITE : PROT_READ;
75 int fd = open(path, open_flags);
76 if (fd == -1) {
77 fprintf(stderr, "Failed to open(): %s\n", strerror(errno));
78 return range;
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;
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;
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;
102 range.start = mem;
103 range.length = stat_buf.st_size;
104 return range;
107 void unmap_file(struct mem_range_t store)
109 if (munmap(store.start, store.length) != 0)
110 fprintf(stderr, "Failed to munmap(): %s\n", strerror(errno));