| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 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. |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Datasheet: |
| 19 | * - Name: Intel 82802AB/82802AC Firmware Hub (FWH) |
| 20 | * - URL: http://www.intel.com/design/chipsets/datashts/290658.htm |
| 21 | * - PDF: http://download.intel.com/design/chipsets/datashts/29065804.pdf |
| 22 | * - Order number: 290658-004 |
| 23 | */ |
| 24 | |
| Felix Singer | 2e003a0 | 2022-08-19 02:36:28 +0200 | [diff] [blame] | 25 | #include <stdbool.h> |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 26 | #include <stdlib.h> |
| 27 | |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 28 | #include "flash.h" |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 29 | #include "programmer.h" |
| Nico Huber | 10337f7 | 2026-03-04 19:57:27 +0100 | [diff] [blame] | 30 | #include "chipdrivers/memory_bus.h" |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 31 | |
| Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 32 | void print_status_82802ab(uint8_t status) |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 33 | { |
| Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 34 | msg_cdbg("%s", status & 0x80 ? "Ready:" : "Busy:"); |
| 35 | msg_cdbg("%s", status & 0x40 ? "BE SUSPEND:" : "BE RUN/FINISH:"); |
| 36 | msg_cdbg("%s", status & 0x20 ? "BE ERROR:" : "BE OK:"); |
| 37 | msg_cdbg("%s", status & 0x10 ? "PROG ERR:" : "PROG OK:"); |
| 38 | msg_cdbg("%s", status & 0x8 ? "VP ERR:" : "VPP OK:"); |
| 39 | msg_cdbg("%s", status & 0x4 ? "PROG SUSPEND:" : "PROG RUN/FINISH:"); |
| 40 | msg_cdbg("%s", status & 0x2 ? "WP|TBL#|WP#,ABORT:" : "UNLOCK:"); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 43 | static struct found_id *probe_82802ab_generic( |
| 44 | const struct par_master *par, |
| 45 | chipsize_t chip_size, feature_bits_t chip_features) |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 46 | { |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 47 | const unsigned int addr_shift = chip_features & FEATURE_ADDR_SHIFTED ? 1 : 0; |
| 48 | uint8_t raw[2], flashcontent1, flashcontent2; |
| 49 | |
| 50 | const chipaddr bios = (chipaddr)programmer_map_flash_data(par, chip_size, ""); |
| 51 | if (bios == (chipaddr)ERROR_PTR) |
| 52 | return NULL; |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 53 | |
| Carl-Daniel Hailfinger | 4e9cebb | 2009-09-05 01:16:30 +0000 | [diff] [blame] | 54 | /* Reset to get a clean state */ |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 55 | par->chip_writeb(par, 0xFF, bios); |
| Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 56 | programmer_delay(10); |
| Carl-Daniel Hailfinger | 4e9cebb | 2009-09-05 01:16:30 +0000 | [diff] [blame] | 57 | |
| 58 | /* Enter ID mode */ |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 59 | par->chip_writeb(par, 0x90, bios); |
| Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 60 | programmer_delay(10); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 61 | |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 62 | raw[0] = par->chip_readb(par, bios + (0x00 << addr_shift)); |
| 63 | raw[1] = par->chip_readb(par, bios + (0x01 << addr_shift)); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 64 | |
| 65 | /* Leave ID mode */ |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 66 | par->chip_writeb(par, 0xFF, bios); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 67 | |
| Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 68 | programmer_delay(10); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 69 | |
| Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 70 | /* |
| 71 | * Read the product ID location again. We should now see normal |
| 72 | * flash contents. |
| 73 | */ |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 74 | flashcontent1 = par->chip_readb(par, bios + (0x00 << addr_shift)); |
| 75 | flashcontent2 = par->chip_readb(par, bios + (0x01 << addr_shift)); |
| Carl-Daniel Hailfinger | 12aa0be | 2010-03-22 23:47:38 +0000 | [diff] [blame] | 76 | |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 77 | programmer_unmap_flash_region(par, (void *)bios, chip_size); |
| 78 | |
| 79 | if (flashprog_no_data(raw, sizeof(raw))) |
| 80 | return NULL; |
| 81 | |
| 82 | msg_cdbg("%s (%uKiB, features: 0x%02x): id1 0x%02x, id2 0x%02x", |
| 83 | __func__, chip_size / KiB, chip_features, raw[0], raw[1]); |
| 84 | |
| 85 | if (!oddparity(raw[0])) |
| 86 | msg_cdbg(", id1 parity violation"); |
| 87 | |
| 88 | if (raw[0] == flashcontent1) |
| Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 89 | msg_cdbg(", id1 is normal flash content"); |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 90 | if (raw[1] == flashcontent2) |
| Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 91 | msg_cdbg(", id2 is normal flash content"); |
| Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 92 | msg_cdbg("\n"); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 93 | |
| Nico Huber | 3a2a4d5 | 2026-03-01 12:15:23 +0100 | [diff] [blame^] | 94 | struct memory_found_id *const found = alloc_memory_found_id(); |
| 95 | if (!found) { |
| 96 | msg_cerr("Out of memory!\n"); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | found->generic.info.id.manufacture = raw[0]; |
| 101 | found->generic.info.id.model = raw[1]; |
| 102 | found->generic.info.id.type = ID_82802AB; |
| 103 | found->memory_info.chip_size = chip_size; |
| 104 | found->memory_info.chip_features = chip_features; |
| 105 | |
| 106 | return &found->generic; |
| 107 | } |
| 108 | |
| 109 | struct found_id *probe_82802ab(const struct bus_probe *probe, |
| 110 | const struct master_common *mst, |
| 111 | const struct flashchip *chip) |
| 112 | { |
| 113 | const struct par_master *const par = (const struct par_master *)mst; |
| 114 | struct found_id *ids = NULL, **next_ptr = &ids; |
| 115 | chipsize_t chip_size; |
| 116 | |
| 117 | if (chip) |
| 118 | return probe_82802ab_generic(par, chip->total_size * KiB, chip->feature_bits); |
| 119 | |
| 120 | for (chip_size = 256*KiB; chip_size <= 2*MiB; chip_size *= 2) { |
| 121 | *next_ptr = probe_82802ab_generic(par, chip_size, 0); |
| 122 | if (*next_ptr) |
| 123 | next_ptr = &(*next_ptr)->next; |
| 124 | |
| 125 | *next_ptr = probe_82802ab_generic(par, chip_size, FEATURE_ADDR_SHIFTED); |
| 126 | if (*next_ptr) |
| 127 | next_ptr = &(*next_ptr)->next; |
| 128 | } |
| 129 | |
| 130 | return ids; |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| Stefan Tauner | 4404f73 | 2013-09-12 08:28:56 +0000 | [diff] [blame] | 133 | /* FIXME: needs timeout */ |
| Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 134 | uint8_t wait_82802ab(struct flashctx *flash) |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 135 | { |
| 136 | uint8_t status; |
| Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 137 | chipaddr bios = flash->virtual_memory; |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 138 | |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 139 | chip_writeb(flash, 0x70, bios); |
| Angel Pons | 68c32db | 2020-01-31 11:16:42 +0100 | [diff] [blame] | 140 | |
| 141 | while ((chip_readb(flash, bios) & 0x80) == 0) // it's busy |
| 142 | ; |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 143 | |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 144 | status = chip_readb(flash, bios); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 145 | |
| Carl-Daniel Hailfinger | 4e9cebb | 2009-09-05 01:16:30 +0000 | [diff] [blame] | 146 | /* Reset to get a clean state */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 147 | chip_writeb(flash, 0xFF, bios); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 148 | |
| 149 | return status; |
| 150 | } |
| 151 | |
| Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 152 | int erase_block_82802ab(struct flashctx *flash, unsigned int page, |
| Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 153 | unsigned int pagesize) |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 154 | { |
| Sean Nelson | 5459637 | 2010-01-09 05:30:14 +0000 | [diff] [blame] | 155 | chipaddr bios = flash->virtual_memory; |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 156 | uint8_t status; |
| 157 | |
| 158 | // clear status register |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 159 | chip_writeb(flash, 0x50, bios + page); |
| Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 160 | |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 161 | // now start it |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 162 | chip_writeb(flash, 0x20, bios + page); |
| 163 | chip_writeb(flash, 0xd0, bios + page); |
| Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 164 | programmer_delay(10); |
| Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 165 | |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 166 | // now let's see what the register is |
| Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 167 | status = wait_82802ab(flash); |
| Sean Nelson | 28accc2 | 2010-03-19 18:47:06 +0000 | [diff] [blame] | 168 | print_status_82802ab(status); |
| Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 169 | |
| Carl-Daniel Hailfinger | b4061f6 | 2011-06-26 17:04:16 +0000 | [diff] [blame] | 170 | /* FIXME: Check the status register for errors. */ |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 174 | /* chunksize is 1 */ |
| Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 175 | int write_82802ab(struct flashctx *flash, const uint8_t *src, unsigned int start, unsigned int len) |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 176 | { |
| Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 177 | unsigned int i; |
| Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 178 | chipaddr dst = flash->virtual_memory + start; |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 179 | |
| Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 180 | for (i = 0; i < len; i++) { |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 181 | /* transfer data from source to destination */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 182 | chip_writeb(flash, 0x40, dst); |
| 183 | chip_writeb(flash, *src++, dst++); |
| Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 184 | wait_82802ab(flash); |
| Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 185 | flashprog_progress_add(flash, 1); |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 186 | } |
| Carl-Daniel Hailfinger | b30a5ed | 2010-10-10 14:02:27 +0000 | [diff] [blame] | 187 | |
| 188 | /* FIXME: Ignore errors for now. */ |
| 189 | return 0; |
| Carl-Daniel Hailfinger | e7bcb19 | 2008-03-14 00:02:25 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 192 | int unlock_28f004s5(struct flashctx *flash) |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 193 | { |
| 194 | chipaddr bios = flash->virtual_memory; |
| Felix Singer | 2e003a0 | 2022-08-19 02:36:28 +0200 | [diff] [blame] | 195 | uint8_t mcfg, bcfg; |
| 196 | bool need_unlock = false, can_unlock = false; |
| Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 197 | unsigned int i; |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 198 | |
| 199 | /* Clear status register */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 200 | chip_writeb(flash, 0x50, bios); |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 201 | |
| 202 | /* Read identifier codes */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 203 | chip_writeb(flash, 0x90, bios); |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 204 | |
| 205 | /* Read master lock-bit */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 206 | mcfg = chip_readb(flash, bios + 0x3); |
| Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 207 | msg_cdbg("master lock is "); |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 208 | if (mcfg) { |
| 209 | msg_cdbg("locked!\n"); |
| 210 | } else { |
| 211 | msg_cdbg("unlocked!\n"); |
| Felix Singer | 2e003a0 | 2022-08-19 02:36:28 +0200 | [diff] [blame] | 212 | can_unlock = true; |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 213 | } |
| Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 214 | |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 215 | /* Read block lock-bits */ |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 216 | for (i = 0; i < flash->chip->total_size * 1024; i+= (64 * 1024)) { |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 217 | bcfg = chip_readb(flash, bios + i + 2); // read block lock config |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 218 | msg_cdbg("block lock at %06x is %slocked!\n", i, bcfg ? "" : "un"); |
| 219 | if (bcfg) { |
| Felix Singer | 2e003a0 | 2022-08-19 02:36:28 +0200 | [diff] [blame] | 220 | need_unlock = true; |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | /* Reset chip */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 225 | chip_writeb(flash, 0xFF, bios); |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 226 | |
| 227 | /* Unlock: clear block lock-bits, if needed */ |
| 228 | if (can_unlock && need_unlock) { |
| Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 229 | msg_cdbg("Unlock: "); |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 230 | chip_writeb(flash, 0x60, bios); |
| 231 | chip_writeb(flash, 0xD0, bios); |
| 232 | chip_writeb(flash, 0xFF, bios); |
| Sean Nelson | ed479d2 | 2010-03-24 23:14:32 +0000 | [diff] [blame] | 233 | msg_cdbg("Done!\n"); |
| Sean Nelson | dee4a83 | 2010-03-22 04:39:31 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* Error: master locked or a block is locked */ |
| 237 | if (!can_unlock && need_unlock) { |
| 238 | msg_cerr("At least one block is locked and lockdown is active!\n"); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 244 | |
| Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 245 | int unlock_lh28f008bjt(struct flashctx *flash) |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 246 | { |
| 247 | chipaddr bios = flash->virtual_memory; |
| 248 | uint8_t mcfg, bcfg; |
| Felix Singer | 2e003a0 | 2022-08-19 02:36:28 +0200 | [diff] [blame] | 249 | bool need_unlock = false, can_unlock = false; |
| Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 250 | unsigned int i; |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 251 | |
| 252 | /* Wait if chip is busy */ |
| 253 | wait_82802ab(flash); |
| 254 | |
| 255 | /* Read identifier codes */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 256 | chip_writeb(flash, 0x90, bios); |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 257 | |
| 258 | /* Read master lock-bit */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 259 | mcfg = chip_readb(flash, bios + 0x3); |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 260 | msg_cdbg("master lock is "); |
| 261 | if (mcfg) { |
| 262 | msg_cdbg("locked!\n"); |
| 263 | } else { |
| 264 | msg_cdbg("unlocked!\n"); |
| Felix Singer | 2e003a0 | 2022-08-19 02:36:28 +0200 | [diff] [blame] | 265 | can_unlock = true; |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* Read block lock-bits, 8 * 8 KB + 15 * 64 KB */ |
| Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 269 | for (i = 0; i < flash->chip->total_size * 1024; |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 270 | i += (i >= (64 * 1024) ? 64 * 1024 : 8 * 1024)) { |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 271 | bcfg = chip_readb(flash, bios + i + 2); /* read block lock config */ |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 272 | msg_cdbg("block lock at %06x is %slocked!\n", i, |
| 273 | bcfg ? "" : "un"); |
| 274 | if (bcfg) |
| Felix Singer | 2e003a0 | 2022-08-19 02:36:28 +0200 | [diff] [blame] | 275 | need_unlock = true; |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* Reset chip */ |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 279 | chip_writeb(flash, 0xFF, bios); |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 280 | |
| 281 | /* Unlock: clear block lock-bits, if needed */ |
| 282 | if (can_unlock && need_unlock) { |
| 283 | msg_cdbg("Unlock: "); |
| Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 284 | chip_writeb(flash, 0x60, bios); |
| 285 | chip_writeb(flash, 0xD0, bios); |
| 286 | chip_writeb(flash, 0xFF, bios); |
| Mattias Mattsson | fca3b01 | 2011-08-25 22:44:11 +0000 | [diff] [blame] | 287 | wait_82802ab(flash); |
| 288 | msg_cdbg("Done!\n"); |
| 289 | } |
| 290 | |
| 291 | /* Error: master locked or a block is locked */ |
| 292 | if (!can_unlock && need_unlock) { |
| 293 | msg_cerr("At least one block is locked and lockdown is active!\n"); |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |