blob: 1f46be14ce14291b99b0e159544239a793a3021d [file] [log] [blame]
Nico Huberc3b02dc2023-08-12 01:13:45 +02001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Google Inc.
5 * Copyright (C) 2012 secunet Security Networks AG
6 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#ifndef __LIBFLASHPROG_H__
20#define __LIBFLASHPROG_H__ 1
21
22#include <sys/types.h>
23#include <stddef.h>
24#include <stdbool.h>
25#include <stdint.h>
26#include <stdarg.h>
27
28int flashprog_init(int perform_selfcheck);
29int flashprog_shutdown(void);
30/** @ingroup flashprog-general */
31enum flashprog_log_level {
32 FLASHPROG_MSG_ERROR = 0,
33 FLASHPROG_MSG_WARN = 1,
34 FLASHPROG_MSG_INFO = 2,
35 FLASHPROG_MSG_DEBUG = 3,
36 FLASHPROG_MSG_DEBUG2 = 4,
37 FLASHPROG_MSG_SPEW = 5,
38};
39/** @ingroup flashprog-general */
40typedef int(flashprog_log_callback)(enum flashprog_log_level, const char *format, va_list);
41void flashprog_set_log_callback(flashprog_log_callback *);
42
43/** @ingroup flashprog-prog */
44struct flashprog_programmer;
45int flashprog_programmer_init(struct flashprog_programmer **, const char *prog_name, const char *prog_params);
46int flashprog_programmer_shutdown(struct flashprog_programmer *);
47
Nico Huber48d4a042026-03-08 16:07:28 +010048struct flashprog_chips;
49__attribute__((nonnull))
50int flashprog_chips_all(struct flashprog_chips **);
51__attribute__((nonnull))
52unsigned int flashprog_chips_count(const struct flashprog_chips *);
53void flashprog_chips_release(struct flashprog_chips *);
54
55struct flashprog_chip;
56__attribute__((nonnull))
57const struct flashprog_chip *flashprog_chip_first(const struct flashprog_chips *);
58__attribute__((nonnull))
59const struct flashprog_chip *flashprog_chip_next(const struct flashprog_chip *);
60__attribute__((nonnull))
61const char *flashprog_chip_vendor(const struct flashprog_chip *);
62__attribute__((nonnull))
63const char *flashprog_chip_name(const struct flashprog_chip *);
64__attribute__((nonnull))
65size_t flashprog_chip_size(const struct flashprog_chip *);
66
Nico Huberc3b02dc2023-08-12 01:13:45 +020067struct flashprog_flashctx;
68int flashprog_flash_probe(struct flashprog_flashctx **, const struct flashprog_programmer *, const char *chip_name);
69size_t flashprog_flash_getsize(const struct flashprog_flashctx *);
70int flashprog_flash_erase(struct flashprog_flashctx *);
71void flashprog_flash_release(struct flashprog_flashctx *);
72
Richard Hughes842d6782021-01-15 09:48:12 +000073enum flashprog_progress_stage {
74 FLASHPROG_PROGRESS_READ,
75 FLASHPROG_PROGRESS_WRITE,
76 FLASHPROG_PROGRESS_ERASE,
77};
78typedef void(flashprog_progress_callback)(enum flashprog_progress_stage, size_t current, size_t total, void *user_data);
79void flashprog_set_progress_callback(struct flashprog_flashctx *, flashprog_progress_callback *, void *user_data);
80
Nico Huberc3b02dc2023-08-12 01:13:45 +020081/** @ingroup flashprog-flash */
82enum flashprog_flag {
83 FLASHPROG_FLAG_FORCE,
84 FLASHPROG_FLAG_FORCE_BOARDMISMATCH,
85 FLASHPROG_FLAG_VERIFY_AFTER_WRITE,
86 FLASHPROG_FLAG_VERIFY_WHOLE_CHIP,
Nico Huber55e78842024-07-21 00:46:19 +020087 FLASHPROG_FLAG_NON_VOLATILE_WRSR,
Nico Huberc3b02dc2023-08-12 01:13:45 +020088};
89void flashprog_flag_set(struct flashprog_flashctx *, enum flashprog_flag, bool value);
90bool flashprog_flag_get(const struct flashprog_flashctx *, enum flashprog_flag);
91
92int flashprog_image_read(struct flashprog_flashctx *, void *buffer, size_t buffer_len);
93int flashprog_image_write(struct flashprog_flashctx *, void *buffer, size_t buffer_len, const void *refbuffer);
94int flashprog_image_verify(struct flashprog_flashctx *, const void *buffer, size_t buffer_len);
95
96struct flashprog_layout;
97int flashprog_layout_new(struct flashprog_layout **);
98int flashprog_layout_read_from_ifd(struct flashprog_layout **, struct flashprog_flashctx *, const void *dump, size_t len);
99int flashprog_layout_read_fmap_from_rom(struct flashprog_layout **,
100 struct flashprog_flashctx *, size_t offset, size_t length);
101int flashprog_layout_read_fmap_from_buffer(struct flashprog_layout **layout,
102 struct flashprog_flashctx *, const uint8_t *buf, size_t len);
103int flashprog_layout_add_region(struct flashprog_layout *, size_t start, size_t end, const char *name);
104int flashprog_layout_include_region(struct flashprog_layout *, const char *name);
Nico Hubereed122d2023-02-11 02:05:07 +0100105int flashprog_layout_get_region_range(const struct flashprog_layout *, const char *name, size_t *start, size_t *len);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200106void flashprog_layout_release(struct flashprog_layout *);
107void flashprog_layout_set(struct flashprog_flashctx *, const struct flashprog_layout *);
108
109/** @ingroup flashprog-wp */
110enum flashprog_wp_result {
111 FLASHPROG_WP_OK = 0,
112 FLASHPROG_WP_ERR_CHIP_UNSUPPORTED = 1,
113 FLASHPROG_WP_ERR_OTHER = 2,
114 FLASHPROG_WP_ERR_READ_FAILED = 3,
115 FLASHPROG_WP_ERR_WRITE_FAILED = 4,
116 FLASHPROG_WP_ERR_VERIFY_FAILED = 5,
117 FLASHPROG_WP_ERR_RANGE_UNSUPPORTED = 6,
118 FLASHPROG_WP_ERR_MODE_UNSUPPORTED = 7,
119 FLASHPROG_WP_ERR_RANGE_LIST_UNAVAILABLE = 8,
120 FLASHPROG_WP_ERR_UNSUPPORTED_STATE = 9,
121};
122
123enum flashprog_wp_mode {
124 FLASHPROG_WP_MODE_DISABLED,
125 FLASHPROG_WP_MODE_HARDWARE,
126 FLASHPROG_WP_MODE_POWER_CYCLE,
127 FLASHPROG_WP_MODE_PERMANENT
128};
129struct flashprog_wp_cfg;
130struct flashprog_wp_ranges;
131
132enum flashprog_wp_result flashprog_wp_cfg_new(struct flashprog_wp_cfg **);
133void flashprog_wp_cfg_release(struct flashprog_wp_cfg *);
134void flashprog_wp_set_mode(struct flashprog_wp_cfg *, enum flashprog_wp_mode);
135enum flashprog_wp_mode flashprog_wp_get_mode(const struct flashprog_wp_cfg *);
136void flashprog_wp_set_range(struct flashprog_wp_cfg *, size_t start, size_t len);
137void flashprog_wp_get_range(size_t *start, size_t *len, const struct flashprog_wp_cfg *);
138
139enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *, struct flashprog_flashctx *);
140enum flashprog_wp_result flashprog_wp_write_cfg(struct flashprog_flashctx *, const struct flashprog_wp_cfg *);
141
142enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **, struct flashprog_flashctx *);
143size_t flashprog_wp_ranges_get_count(const struct flashprog_wp_ranges *);
144enum flashprog_wp_result flashprog_wp_ranges_get_range(size_t *start, size_t *len, const struct flashprog_wp_ranges *, unsigned int index);
145void flashprog_wp_ranges_release(struct flashprog_wp_ranges *);
146
147#endif /* !__LIBFLASHPROG_H__ */