Nikolai Artemiev | c6c3f28 | 2021-10-20 23:34:15 +1100 | [diff] [blame] | 1 | /* |
| 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 Artemiev | e75127a | 2021-10-21 01:12:39 +1100 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <stddef.h> |
| 24 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 25 | #include "libflashprog.h" |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 26 | |
Nikolai Artemiev | c6c3f28 | 2021-10-20 23:34:15 +1100 | [diff] [blame] | 27 | #define MAX_BP_BITS 4 |
| 28 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 29 | /* Chip protection range: start address and length. */ |
| 30 | struct wp_range { |
| 31 | size_t start, len; |
| 32 | }; |
| 33 | |
| 34 | /* Generic description of a chip's write protection configuration. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 35 | struct flashprog_wp_cfg { |
| 36 | enum flashprog_wp_mode mode; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 37 | struct wp_range range; |
| 38 | }; |
| 39 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 40 | /* Collection of multiple write protection ranges. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 41 | struct flashprog_wp_ranges { |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 42 | struct wp_range *ranges; |
| 43 | size_t count; |
| 44 | }; |
| 45 | |
Nikolai Artemiev | e75127a | 2021-10-21 01:12:39 +1100 | [diff] [blame] | 46 | /* |
| 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 | */ |
| 52 | struct 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 78 | struct flashprog_flashctx; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 79 | |
| 80 | /* Write WP configuration to the chip */ |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 81 | enum flashprog_wp_result spi_wp_write_cfg(struct flashprog_flashctx *, const struct flashprog_wp_cfg *); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 82 | |
| 83 | /* Read WP configuration from the chip */ |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 84 | enum flashprog_wp_result spi_wp_read_cfg(struct flashprog_wp_cfg *, struct flashprog_flashctx *); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 85 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 86 | /* Get a list of protection ranges supported by the chip */ |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 87 | enum flashprog_wp_result spi_wp_get_available_ranges(struct flashprog_wp_ranges **, struct flashprog_flashctx *); |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 88 | |
Nikolai Artemiev | c6c3f28 | 2021-10-20 23:34:15 +1100 | [diff] [blame] | 89 | #endif /* !__WRITEPROTECT_H__ */ |