Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2008 coresystems GmbH |
| 5 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "flash.h" |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 23 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 24 | static int printlock_w39_fwh_block(struct flashctx *flash, unsigned int offset) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 25 | { |
| 26 | chipaddr wrprotect = flash->virtual_registers + offset + 2; |
| 27 | uint8_t locking; |
| 28 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 29 | locking = chip_readb(flash, wrprotect); |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 30 | msg_cdbg("Lock status of block at 0x%08x is ", offset); |
| 31 | switch (locking & 0x7) { |
| 32 | case 0: |
| 33 | msg_cdbg("Full Access.\n"); |
| 34 | break; |
| 35 | case 1: |
| 36 | msg_cdbg("Write Lock (Default State).\n"); |
| 37 | break; |
| 38 | case 2: |
| 39 | msg_cdbg("Locked Open (Full Access, Lock Down).\n"); |
| 40 | break; |
| 41 | case 3: |
| 42 | msg_cerr("Error: Write Lock, Locked Down.\n"); |
| 43 | break; |
| 44 | case 4: |
| 45 | msg_cdbg("Read Lock.\n"); |
| 46 | break; |
| 47 | case 5: |
| 48 | msg_cdbg("Read/Write Lock.\n"); |
| 49 | break; |
| 50 | case 6: |
| 51 | msg_cerr("Error: Read Lock, Locked Down.\n"); |
| 52 | break; |
| 53 | case 7: |
| 54 | msg_cerr("Error: Read/Write Lock, Locked Down.\n"); |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | /* Read or write lock present? */ |
| 59 | return (locking & ((1 << 2) | (1 << 0))) ? -1 : 0; |
| 60 | } |
| 61 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 62 | static int unlock_w39_fwh_block(struct flashctx *flash, unsigned int offset) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 63 | { |
| 64 | chipaddr wrprotect = flash->virtual_registers + offset + 2; |
| 65 | uint8_t locking; |
| 66 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 67 | locking = chip_readb(flash, wrprotect); |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 68 | /* Read or write lock present? */ |
| 69 | if (locking & ((1 << 2) | (1 << 0))) { |
| 70 | /* Lockdown active? */ |
| 71 | if (locking & (1 << 1)) { |
Stefan Tauner | 716e098 | 2011-07-25 20:38:52 +0000 | [diff] [blame] | 72 | msg_cerr("Can't unlock block at 0x%08x!\n", offset); |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 73 | return -1; |
| 74 | } else { |
Stefan Tauner | 716e098 | 2011-07-25 20:38:52 +0000 | [diff] [blame] | 75 | msg_cdbg("Unlocking block at 0x%08x\n", offset); |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 76 | chip_writeb(flash, 0, wrprotect); |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 83 | static uint8_t w39_idmode_readb(struct flashctx *flash, unsigned int offset) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 84 | { |
| 85 | chipaddr bios = flash->virtual_memory; |
| 86 | uint8_t val; |
| 87 | |
| 88 | /* Product Identification Entry */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 89 | chip_writeb(flash, 0xAA, bios + 0x5555); |
| 90 | chip_writeb(flash, 0x55, bios + 0x2AAA); |
| 91 | chip_writeb(flash, 0x90, bios + 0x5555); |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 92 | programmer_delay(10); |
| 93 | |
| 94 | /* Read something, maybe hardware lock bits */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 95 | val = chip_readb(flash, bios + offset); |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 96 | |
| 97 | /* Product Identification Exit */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 98 | chip_writeb(flash, 0xAA, bios + 0x5555); |
| 99 | chip_writeb(flash, 0x55, bios + 0x2AAA); |
| 100 | chip_writeb(flash, 0xF0, bios + 0x5555); |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 101 | programmer_delay(10); |
| 102 | |
| 103 | return val; |
| 104 | } |
| 105 | |
| 106 | static int printlock_w39_tblwp(uint8_t lock) |
| 107 | { |
| 108 | msg_cdbg("Hardware bootblock locking (#TBL) is %sactive.\n", |
| 109 | (lock & (1 << 2)) ? "" : "not "); |
| 110 | msg_cdbg("Hardware remaining chip locking (#WP) is %sactive..\n", |
| 111 | (lock & (1 << 3)) ? "" : "not "); |
| 112 | if (lock & ((1 << 2) | (1 << 3))) |
| 113 | return -1; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Kyösti Mälkki | c31243e | 2012-10-28 01:50:08 +0000 | [diff] [blame^] | 118 | static int printlock_w39_single_bootblock(uint8_t lock, uint16_t kB) |
| 119 | { |
| 120 | msg_cdbg("Software %d kB bootblock locking is %sactive.\n", kB, (lock & 0x03) ? "" : "not "); |
| 121 | if (lock & 0x03) |
| 122 | return -1; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 127 | static int printlock_w39_bootblock_64k16k(uint8_t lock) |
| 128 | { |
| 129 | msg_cdbg("Software 64 kB bootblock locking is %sactive.\n", |
| 130 | (lock & (1 << 0)) ? "" : "not "); |
| 131 | msg_cdbg("Software 16 kB bootblock locking is %sactive.\n", |
| 132 | (lock & (1 << 1)) ? "" : "not "); |
| 133 | if (lock & ((1 << 1) | (1 << 0))) |
| 134 | return -1; |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 139 | static int printlock_w39_common(struct flashctx *flash, unsigned int offset) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 140 | { |
| 141 | uint8_t lock; |
| 142 | |
| 143 | lock = w39_idmode_readb(flash, offset); |
| 144 | msg_cdbg("Lockout bits:\n"); |
| 145 | return printlock_w39_tblwp(lock); |
| 146 | } |
| 147 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 148 | static int printlock_w39_fwh(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 149 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 150 | unsigned int i, total_size = flash->chip->total_size * 1024; |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 151 | int ret = 0; |
| 152 | |
| 153 | /* Print lock status of the complete chip */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 154 | for (i = 0; i < total_size; i += flash->chip->page_size) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 155 | ret |= printlock_w39_fwh_block(flash, i); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 160 | static int unlock_w39_fwh(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 161 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 162 | unsigned int i, total_size = flash->chip->total_size * 1024; |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 163 | |
| 164 | /* Unlock the complete chip */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 165 | for (i = 0; i < total_size; i += flash->chip->page_size) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 166 | if (unlock_w39_fwh_block(flash, i)) |
| 167 | return -1; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Kyösti Mälkki | c31243e | 2012-10-28 01:50:08 +0000 | [diff] [blame^] | 172 | int printlock_w39f010(struct flashctx *flash) |
| 173 | { |
| 174 | uint8_t lock; |
| 175 | int ret; |
| 176 | |
| 177 | lock = w39_idmode_readb(flash, 0x00002); |
| 178 | msg_cdbg("Bottom boot block:\n"); |
| 179 | ret = printlock_w39_single_bootblock(lock, 16); |
| 180 | |
| 181 | lock = w39_idmode_readb(flash, 0x1fff2); |
| 182 | msg_cdbg("Top boot block:\n"); |
| 183 | ret |= printlock_w39_single_bootblock(lock, 16); |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | int printlock_w39l010(struct flashctx *flash) |
| 189 | { |
| 190 | uint8_t lock; |
| 191 | int ret; |
| 192 | |
| 193 | lock = w39_idmode_readb(flash, 0x00002); |
| 194 | msg_cdbg("Bottom boot block:\n"); |
| 195 | ret = printlock_w39_single_bootblock(lock, 8); |
| 196 | |
| 197 | lock = w39_idmode_readb(flash, 0x1fff2); |
| 198 | msg_cdbg("Top boot block:\n"); |
| 199 | ret |= printlock_w39_single_bootblock(lock, 8); |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | int printlock_w39l020(struct flashctx *flash) |
| 205 | { |
| 206 | uint8_t lock; |
| 207 | int ret; |
| 208 | |
| 209 | lock = w39_idmode_readb(flash, 0x00002); |
| 210 | msg_cdbg("Bottom boot block:\n"); |
| 211 | ret = printlock_w39_bootblock_64k16k(lock); |
| 212 | |
| 213 | lock = w39_idmode_readb(flash, 0x3fff2); |
| 214 | msg_cdbg("Top boot block:\n"); |
| 215 | ret |= printlock_w39_bootblock_64k16k(lock); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 220 | int printlock_w39l040(struct flashctx *flash) |
Michael Karcher | 19e0aac | 2011-03-06 17:58:05 +0000 | [diff] [blame] | 221 | { |
| 222 | uint8_t lock; |
| 223 | int ret; |
| 224 | |
| 225 | lock = w39_idmode_readb(flash, 0x00002); |
| 226 | msg_cdbg("Bottom boot block:\n"); |
| 227 | ret = printlock_w39_bootblock_64k16k(lock); |
| 228 | |
| 229 | lock = w39_idmode_readb(flash, 0x7fff2); |
| 230 | msg_cdbg("Top boot block:\n"); |
| 231 | ret |= printlock_w39_bootblock_64k16k(lock); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 236 | int printlock_w39v040a(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 237 | { |
| 238 | uint8_t lock; |
| 239 | int ret = 0; |
| 240 | |
| 241 | /* The W39V040A datasheet contradicts itself on the lock register |
| 242 | * location: 0x00002 and 0x7fff2 are both mentioned. Pick the one |
| 243 | * which is similar to the other chips of the same family. |
| 244 | */ |
| 245 | lock = w39_idmode_readb(flash, 0x7fff2); |
| 246 | msg_cdbg("Lockout bits:\n"); |
| 247 | |
| 248 | ret = printlock_w39_tblwp(lock); |
| 249 | ret |= printlock_w39_bootblock_64k16k(lock); |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 254 | int printlock_w39v040b(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 255 | { |
| 256 | return printlock_w39_common(flash, 0x7fff2); |
| 257 | } |
| 258 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 259 | int printlock_w39v040c(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 260 | { |
| 261 | /* Typo in the datasheet? The other chips use 0x7fff2. */ |
| 262 | return printlock_w39_common(flash, 0xfff2); |
| 263 | } |
| 264 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 265 | int printlock_w39v040fa(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 266 | { |
| 267 | int ret = 0; |
| 268 | |
| 269 | ret = printlock_w39v040a(flash); |
| 270 | ret |= printlock_w39_fwh(flash); |
| 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 275 | int printlock_w39v040fb(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 276 | { |
| 277 | int ret = 0; |
| 278 | |
| 279 | ret = printlock_w39v040b(flash); |
| 280 | ret |= printlock_w39_fwh(flash); |
| 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 285 | int printlock_w39v040fc(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 286 | { |
| 287 | int ret = 0; |
| 288 | |
| 289 | /* W39V040C and W39V040FC use different WP/TBL offsets. */ |
| 290 | ret = printlock_w39_common(flash, 0x7fff2); |
| 291 | ret |= printlock_w39_fwh(flash); |
| 292 | |
| 293 | return ret; |
| 294 | } |
| 295 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 296 | int printlock_w39v080a(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 297 | { |
| 298 | return printlock_w39_common(flash, 0xffff2); |
| 299 | } |
| 300 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 301 | int printlock_w39v080fa(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 302 | { |
| 303 | int ret = 0; |
| 304 | |
| 305 | ret = printlock_w39v080a(flash); |
| 306 | ret |= printlock_w39_fwh(flash); |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 311 | int printlock_w39v080fa_dual(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 312 | { |
| 313 | msg_cinfo("Block locking for W39V080FA in dual mode is " |
| 314 | "undocumented.\n"); |
| 315 | /* Better safe than sorry. */ |
| 316 | return -1; |
| 317 | } |
| 318 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 319 | int unlock_w39v040fb(struct flashctx *flash) |
Idwer Vollering | ecc6707 | 2010-12-26 23:55:12 +0000 | [diff] [blame] | 320 | { |
| 321 | if (unlock_w39_fwh(flash)) |
| 322 | return -1; |
| 323 | if (printlock_w39_common(flash, 0x7fff2)) |
| 324 | return -1; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 329 | int unlock_w39v080fa(struct flashctx *flash) |
Carl-Daniel Hailfinger | 9188240 | 2010-12-05 16:33:59 +0000 | [diff] [blame] | 330 | { |
| 331 | if (unlock_w39_fwh(flash)) |
| 332 | return -1; |
| 333 | if (printlock_w39_common(flash, 0xffff2)) |
| 334 | return -1; |
| 335 | |
| 336 | return 0; |
| 337 | } |
David Borg | f5a30f6 | 2012-04-15 13:16:32 +0000 | [diff] [blame] | 338 | |
| 339 | int printlock_at49f(struct flashctx *flash) |
| 340 | { |
| 341 | uint8_t lock = w39_idmode_readb(flash, 0x00002); |
| 342 | msg_cdbg("Hardware bootblock lockout is %sactive.\n", |
| 343 | (lock & 0x01) ? "" : "not "); |
| 344 | return 0; |
| 345 | } |