1 /* Copyright 2020 The ChromiumOS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
7 #define _POSIX_C_SOURCE 200809L
16 #include <sys/types.h>
19 #include "common.h" /* from cbfstool for buffer API. */
20 #include "subprocess.h" /* from vboot_reference */
23 #define FLASHROM_EXEC_NAME "flashrom"
24 #define FLASHROM_PROGRAMMER_INTERNAL_AP "internal"
27 * Helper to create a temporary file.
29 * @param path_out An output pointer for the filename. Caller should free.
31 * @return 0 on success, -1 for file open error, or -2 for write error.
33 static int create_temp_file(char **path_out
)
40 #if defined(__FreeBSD__)
41 #define P_tmpdir "/tmp"
44 path
= strdup(P_tmpdir
"/flashrom.XXXXXX");
45 /* Set the umask before mkstemp for security considerations. */
46 umask_save
= umask(077);
63 static int run_flashrom(const char *const argv
[])
65 int status
= subprocess_run(argv
, &subprocess_null
, &subprocess_null
,
68 fprintf(stderr
, "Flashrom invocation failed (exit status %d):",
70 for (const char *const *argp
= argv
; *argp
; argp
++)
71 fprintf(stderr
, " %s", *argp
);
72 fprintf(stderr
, "\n");
79 int flashrom_host_read(struct buffer
*buffer
, const char *region
)
82 char region_param
[PATH_MAX
];
85 if (create_temp_file(&tmpfile
) != 0)
88 snprintf(region_param
, sizeof(region_param
), "%s:%s", region
,
90 const char *const argv
[] = {
93 FLASHROM_PROGRAMMER_INTERNAL_AP
,
95 region
? "-i" : tmpfile
,
96 region
? region_param
: NULL
,
99 rv
= run_flashrom(argv
);
101 rv
= buffer_from_file(buffer
, tmpfile
);
109 int flashrom_host_write(struct buffer
*buffer
, const char *region
)
112 char region_param
[PATH_MAX
];
115 if (create_temp_file(&tmpfile
) != 0)
117 if (buffer_write_file(buffer
, tmpfile
) != 0) {
123 snprintf(region_param
, sizeof(region_param
), "%s:%s", region
,
125 const char *const argv
[] = {
128 FLASHROM_PROGRAMMER_INTERNAL_AP
,
131 region
? "-i" : tmpfile
,
132 region
? region_param
: NULL
,
136 rv
= run_flashrom(argv
);