blob: 356ffb20b231357da88131c54e4fe879766f7a70 [file] [log] [blame]
Nikolai Artemievc6c3f282021-10-20 23:34:15 +11001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#ifndef __WRITEPROTECT_H__
19#define __WRITEPROTECT_H__ 1
20
Nikolai Artemieve75127a2021-10-21 01:12:39 +110021#include <stdint.h>
22#include <stdbool.h>
23#include <stddef.h>
24
Nico Huberc3b02dc2023-08-12 01:13:45 +020025#include "libflashprog.h"
Nikolai Artemievda1c8342021-10-21 00:58:12 +110026
Nikolai Artemievc6c3f282021-10-20 23:34:15 +110027#define MAX_BP_BITS 4
28
Nikolai Artemievda1c8342021-10-21 00:58:12 +110029/* Chip protection range: start address and length. */
30struct wp_range {
31 size_t start, len;
32};
33
34/* Generic description of a chip's write protection configuration. */
Nico Huberc3b02dc2023-08-12 01:13:45 +020035struct flashprog_wp_cfg {
36 enum flashprog_wp_mode mode;
Nikolai Artemievda1c8342021-10-21 00:58:12 +110037 struct wp_range range;
38};
39
Nikolai Artemiev077c0d12021-10-21 01:50:15 +110040/* Collection of multiple write protection ranges. */
Nico Huberc3b02dc2023-08-12 01:13:45 +020041struct flashprog_wp_ranges {
Nikolai Artemiev077c0d12021-10-21 01:50:15 +110042 struct wp_range *ranges;
43 size_t count;
44};
45
Nikolai Artemieve75127a2021-10-21 01:12:39 +110046/*
47 * Description of a chip's write protection configuration.
48 *
49 * It allows most WP code to store and manipulate a chip's configuration
50 * without knowing the exact layout of bits in the chip's status registers.
51 */
52struct wp_bits {
53 /* Status register protection bit (SRP) */
54 bool srp_bit_present;
55 uint8_t srp;
56
57 /* Status register lock bit (SRL) */
58 bool srl_bit_present;
59 uint8_t srl;
60
61 /* Complement bit (CMP) */
62 bool cmp_bit_present;
63 uint8_t cmp;
64
65 /* Sector/block protection bit (SEC) */
66 bool sec_bit_present;
67 uint8_t sec;
68
69 /* Top/bottom protection bit (TB) */
70 bool tb_bit_present;
71 uint8_t tb;
72
73 /* Block protection bits (BP) */
74 size_t bp_bit_count;
75 uint8_t bp[MAX_BP_BITS];
76};
77
Nico Huberc3b02dc2023-08-12 01:13:45 +020078struct flashprog_flashctx;
Nikolai Artemievda1c8342021-10-21 00:58:12 +110079
80/* Write WP configuration to the chip */
Nico Huberaabb3e02023-01-13 00:22:30 +010081enum flashprog_wp_result spi_wp_write_cfg(struct flashprog_flashctx *, const struct flashprog_wp_cfg *);
Nikolai Artemievda1c8342021-10-21 00:58:12 +110082
83/* Read WP configuration from the chip */
Nico Huberaabb3e02023-01-13 00:22:30 +010084enum flashprog_wp_result spi_wp_read_cfg(struct flashprog_wp_cfg *, struct flashprog_flashctx *);
Nikolai Artemievda1c8342021-10-21 00:58:12 +110085
Nikolai Artemiev077c0d12021-10-21 01:50:15 +110086/* Get a list of protection ranges supported by the chip */
Nico Huberaabb3e02023-01-13 00:22:30 +010087enum flashprog_wp_result spi_wp_get_available_ranges(struct flashprog_wp_ranges **, struct flashprog_flashctx *);
Nikolai Artemiev077c0d12021-10-21 01:50:15 +110088
Nikolai Artemievc6c3f282021-10-20 23:34:15 +110089#endif /* !__WRITEPROTECT_H__ */