forked from flashrom/flashrom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashrom.c
2048 lines (1834 loc) · 61.9 KB
/
flashrom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the flashrom project.
*
* Copyright (C) 2000 Silicon Integrated System Corporation
* Copyright (C) 2004 Tyan Corp <[email protected]>
* Copyright (C) 2005-2008 coresystems GmbH
* Copyright (C) 2008,2009 Carl-Daniel Hailfinger
* Copyright (C) 2016 secunet Security Networks AG
* (Written by Nico Huber <[email protected]> for secunet)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "flash.h"
#include "flashchips.h"
#include "programmer.h"
#include "hwaccess_physmap.h"
#include "chipdrivers.h"
#include "erasure_layout.h"
const char flashrom_version[] = FLASHROM_VERSION;
static const struct programmer_entry *programmer = NULL;
/*
* Programmers supporting multiple buses can have differing size limits on
* each bus. Store the limits for each bus in a common struct.
*/
struct decode_sizes max_rom_decode;
/* If nonzero, used as the start address of bottom-aligned flash. */
uintptr_t flashbase;
/* Is writing allowed with this programmer? */
bool programmer_may_write;
#define SHUTDOWN_MAXFN 32
static int shutdown_fn_count = 0;
/** @private */
static struct shutdown_func_data {
int (*func) (void *data);
void *data;
} shutdown_fn[SHUTDOWN_MAXFN];
/* Initialize to 0 to make sure nobody registers a shutdown function before
* programmer init.
*/
static bool may_register_shutdown = false;
static struct bus_type_info {
enum chipbustype type;
const char *name;
} bustypes[] = {
{ BUS_PARALLEL, "Parallel, " },
{ BUS_LPC, "LPC, " },
{ BUS_FWH, "FWH, " },
{ BUS_SPI, "SPI, " },
{ BUS_PROG, "Programmer-specific, " },
};
/* Register a function to be executed on programmer shutdown.
* The advantage over atexit() is that you can supply a void pointer which will
* be used as parameter to the registered function upon programmer shutdown.
* This pointer can point to arbitrary data used by said function, e.g. undo
* information for GPIO settings etc. If unneeded, set data=NULL.
* Please note that the first (void *data) belongs to the function signature of
* the function passed as first parameter.
*/
int register_shutdown(int (*function) (void *data), void *data)
{
if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
msg_perr("Tried to register more than %i shutdown functions.\n",
SHUTDOWN_MAXFN);
return 1;
}
if (!may_register_shutdown) {
msg_perr("Tried to register a shutdown function before "
"programmer init.\n");
return 1;
}
shutdown_fn[shutdown_fn_count].func = function;
shutdown_fn[shutdown_fn_count].data = data;
shutdown_fn_count++;
return 0;
}
int register_chip_restore(chip_restore_fn_cb_t func,
struct flashctx *flash, void *data)
{
if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
msg_perr("Tried to register more than %i chip restore"
" functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
return 1;
}
flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
flash->chip_restore_fn[flash->chip_restore_fn_count].data = data;
flash->chip_restore_fn_count++;
return 0;
}
static int deregister_chip_restore(struct flashctx *flash)
{
int rc = 0;
while (flash->chip_restore_fn_count > 0) {
int i = --flash->chip_restore_fn_count;
rc |= flash->chip_restore_fn[i].func(
flash, flash->chip_restore_fn[i].data);
}
return rc;
}
int programmer_init(const struct programmer_entry *prog, const char *param)
{
int ret;
if (prog == NULL) {
msg_perr("Invalid programmer specified!\n");
return -1;
}
programmer = prog;
/* Initialize all programmer specific data. */
/* Default to unlimited decode sizes. */
max_rom_decode = (const struct decode_sizes) {
.parallel = 0xffffffff,
.lpc = 0xffffffff,
.fwh = 0xffffffff,
.spi = 0xffffffff,
};
/* Default to top aligned flash at 4 GB. */
flashbase = 0;
/* Registering shutdown functions is now allowed. */
may_register_shutdown = true;
/* Default to allowing writes. Broken programmers set this to 0. */
programmer_may_write = true;
struct programmer_cfg cfg;
if (param) {
cfg.params = strdup(param);
if (!cfg.params) {
msg_perr("Out of memory!\n");
return ERROR_FLASHROM_FATAL;
}
} else {
cfg.params = NULL;
}
msg_pdbg("Initializing %s programmer\n", prog->name);
ret = prog->init(&cfg);
if (cfg.params && strlen(cfg.params)) {
if (ret != 0) {
/* It is quite possible that any unhandled programmer parameter would have been valid,
* but an error in actual programmer init happened before the parameter was evaluated.
*/
msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
cfg.params);
} else {
/* Actual programmer init was successful, but the user specified an invalid or unusable
* (for the current programmer configuration) parameter.
*/
msg_perr("Unhandled programmer parameters: %s\n", cfg.params);
msg_perr("Aborting.\n");
ret = ERROR_FLASHROM_FATAL;
}
}
free(cfg.params);
return ret;
}
/** Calls registered shutdown functions and resets internal programmer-related variables.
* Calling it is safe even without previous initialization, but further interactions with programmer support
* require a call to programmer_init() (afterwards).
*
* @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
int programmer_shutdown(void)
{
int ret = 0;
/* Registering shutdown functions is no longer allowed. */
may_register_shutdown = false;
while (shutdown_fn_count > 0) {
int i = --shutdown_fn_count;
ret |= shutdown_fn[i].func(shutdown_fn[i].data);
}
registered_master_count = 0;
return ret;
}
void *master_map_flash_region(const struct registered_master *mst,
const char *descr, uintptr_t phys_addr,
size_t len)
{
/* Check the bus master for a specialized map_flash_region; default to
* fallback if it does not specialize it
*/
void *(*map_flash_region) (const char *descr, uintptr_t phys_addr, size_t len) = NULL;
if (mst->buses_supported & BUS_SPI)
map_flash_region = mst->spi.map_flash_region;
else if (mst->buses_supported & BUS_NONSPI)
map_flash_region = mst->par.map_flash_region;
/* A result of NULL causes mapped addresses to be chip physical
* addresses, assuming only a single region is mapped (the entire flash
* space). Chips with a second region (like a register map) require a
* real memory mapping to distinguish the different ranges. Those chips
* are FWH/LPC, so the bus master provides a real mapping.
*/
void *ret = NULL;
if (map_flash_region)
ret = map_flash_region(descr, phys_addr, len);
msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
__func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
return ret;
}
void master_unmap_flash_region(const struct registered_master *mst,
void *virt_addr, size_t len)
{
void (*unmap_flash_region) (void *virt_addr, size_t len) = NULL;
if (mst->buses_supported & BUS_SPI)
unmap_flash_region = mst->spi.unmap_flash_region;
else if (mst->buses_supported & BUS_NONSPI)
unmap_flash_region = mst->par.unmap_flash_region;
if (unmap_flash_region)
unmap_flash_region(virt_addr, len);
msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
}
static bool master_uses_physmap(const struct registered_master *mst)
{
#if CONFIG_INTERNAL == 1
if (mst->buses_supported & BUS_SPI)
return mst->spi.map_flash_region == physmap;
else if (mst->buses_supported & BUS_NONSPI)
return mst->par.map_flash_region == physmap;
#endif
return false;
}
void programmer_delay(const struct flashctx *flash, unsigned int usecs)
{
if (usecs == 0)
return;
/**
* Drivers should either use default_delay() directly or their
* own custom delay. Only core flashrom logic calls programmer_delay()
* which should always have a valid flash context. A NULL context
* more than likely indicates a layering violation or BUG however
* for now dispatch a default_delay() as a safe default for the NULL
* base case.
*/
if (!flash) {
msg_perr("%s called with NULL flash context. "
"Please report a bug at [email protected]\n",
__func__);
return default_delay(usecs);
}
if (flash->mst->buses_supported & BUS_SPI) {
if (flash->mst->spi.delay)
return flash->mst->spi.delay(flash, usecs);
} else if (flash->mst->buses_supported & BUS_PARALLEL) {
if (flash->mst->par.delay)
return flash->mst->par.delay(flash, usecs);
} else if (flash->mst->buses_supported & BUS_PROG) {
if (flash->mst->opaque.delay)
return flash->mst->opaque.delay(flash, usecs);
}
return default_delay(usecs);
}
int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
int unsigned len)
{
chip_readn(flash, buf, flash->virtual_memory + start, len);
return 0;
}
/* This is a somewhat hacked function similar in some ways to strtok().
* It will look for needle with a subsequent '=' in haystack, return a copy of
* needle and remove everything from the first occurrence of needle to the next
* delimiter from haystack.
*/
static char *extract_param(char *const *haystack, const char *needle, const char *delim)
{
char *param_pos, *opt_pos, *rest;
char *opt = NULL;
int optlen;
int needlelen;
needlelen = strlen(needle);
if (!needlelen) {
msg_gerr("%s: empty needle! Please report a bug at "
"[email protected]\n", __func__);
return NULL;
}
/* No programmer parameters given. */
if (*haystack == NULL)
return NULL;
param_pos = strstr(*haystack, needle);
do {
if (!param_pos)
return NULL;
/* Needle followed by '='? */
if (param_pos[needlelen] == '=') {
/* Beginning of the string? */
if (param_pos == *haystack)
break;
/* After a delimiter? */
if (strchr(delim, *(param_pos - 1)))
break;
}
/* Continue searching. */
param_pos++;
param_pos = strstr(param_pos, needle);
} while (1);
if (param_pos) {
/* Get the string after needle and '='. */
opt_pos = param_pos + needlelen + 1;
optlen = strcspn(opt_pos, delim);
/* Return an empty string if the parameter was empty. */
opt = malloc(optlen + 1);
if (!opt) {
msg_gerr("Out of memory!\n");
return NULL;
}
strncpy(opt, opt_pos, optlen);
opt[optlen] = '\0';
rest = opt_pos + optlen;
/* Skip all delimiters after the current parameter. */
rest += strspn(rest, delim);
memmove(param_pos, rest, strlen(rest) + 1);
/* We could shrink haystack, but the effort is not worth it. */
}
return opt;
}
char *extract_programmer_param_str(const struct programmer_cfg *cfg, const char *param_name)
{
return extract_param(&cfg->params, param_name, ",");
}
void get_flash_region(const struct flashctx *flash, int addr, struct flash_region *region)
{
if ((flash->mst->buses_supported & BUS_PROG) && flash->mst->opaque.get_region) {
flash->mst->opaque.get_region(flash, addr, region);
} else if (flash->mst->buses_supported & BUS_SPI && flash->mst->spi.get_region) {
flash->mst->spi.get_region(flash, addr, region);
} else {
region->name = strdup("");
region->start = 0;
region->end = flashrom_flash_getsize(flash) - 1;
region->read_prot = false;
region->write_prot = false;
}
}
int check_for_unwritable_regions(const struct flashctx *flash, unsigned int start, unsigned int len)
{
struct flash_region region;
for (unsigned int addr = start; addr < start + len; addr = region.end + 1) {
get_flash_region(flash, addr, ®ion);
if (region.write_prot) {
msg_gerr("%s: cannot write/erase inside %s region (%#08"PRIx32"..%#08"PRIx32").\n",
__func__, region.name, region.start, region.end);
free(region.name);
return -1;
}
free(region.name);
}
return 0;
}
#ifdef FLASHROM_TEST
/* special unit-test hooks */
erasefunc_t *g_test_erase_injector[NUM_TEST_ERASE_INJECTORS];
#endif
erasefunc_t *lookup_erase_func_ptr(const struct block_eraser *const eraser)
{
switch (eraser->block_erase) {
case SPI_BLOCK_ERASE_EMULATION: return &spi_block_erase_emulation;
case SPI_BLOCK_ERASE_20: return &spi_block_erase_20;
case SPI_BLOCK_ERASE_21: return &spi_block_erase_21;
case SPI_BLOCK_ERASE_40: return NULL; // FIXME unhandled &spi_block_erase_40;
case SPI_BLOCK_ERASE_50: return &spi_block_erase_50;
case SPI_BLOCK_ERASE_52: return &spi_block_erase_52;
case SPI_BLOCK_ERASE_53: return &spi_block_erase_53;
case SPI_BLOCK_ERASE_5C: return &spi_block_erase_5c;
case SPI_BLOCK_ERASE_60: return &spi_block_erase_60;
case SPI_BLOCK_ERASE_62: return &spi_block_erase_62;
case SPI_BLOCK_ERASE_81: return &spi_block_erase_81;
case SPI_BLOCK_ERASE_C4: return &spi_block_erase_c4;
case SPI_BLOCK_ERASE_C7: return &spi_block_erase_c7;
case SPI_BLOCK_ERASE_D7: return &spi_block_erase_d7;
case SPI_BLOCK_ERASE_D8: return &spi_block_erase_d8;
case SPI_BLOCK_ERASE_DB: return &spi_block_erase_db;
case SPI_BLOCK_ERASE_DC: return &spi_block_erase_dc;
case S25FL_BLOCK_ERASE: return &s25fl_block_erase;
case S25FS_BLOCK_ERASE_D8: return &s25fs_block_erase_d8;
case JEDEC_SECTOR_ERASE: return &erase_sector_jedec; // TODO rename to &jedec_sector_erase;
case JEDEC_BLOCK_ERASE: return &erase_block_jedec; // TODO rename to &jedec_block_erase;
case JEDEC_CHIP_BLOCK_ERASE: return &erase_chip_block_jedec; // TODO rename to &jedec_chip_block_erase;
case OPAQUE_ERASE: return &erase_opaque; // TODO rename to &opqaue_erase;
case SPI_ERASE_AT45CS_SECTOR: return &spi_erase_at45cs_sector;
case SPI_ERASE_AT45DB_BLOCK: return &spi_erase_at45db_block;
case SPI_ERASE_AT45DB_CHIP: return &spi_erase_at45db_chip;
case SPI_ERASE_AT45DB_PAGE: return &spi_erase_at45db_page;
case SPI_ERASE_AT45DB_SECTOR: return &spi_erase_at45db_sector;
case ERASE_CHIP_28SF040: return &erase_chip_28sf040;
case ERASE_SECTOR_28SF040: return &erase_sector_28sf040;
case ERASE_BLOCK_82802AB: return &erase_block_82802ab;
case ERASE_SECTOR_49LFXXXC: return &erase_sector_49lfxxxc;
case STM50_SECTOR_ERASE: return &erase_sector_stm50; // TODO rename to &stm50_sector_erase;
case EDI_CHIP_BLOCK_ERASE: return &edi_chip_block_erase;
#ifdef FLASHROM_TEST
case TEST_ERASE_INJECTOR_1:
case TEST_ERASE_INJECTOR_2:
case TEST_ERASE_INJECTOR_3:
case TEST_ERASE_INJECTOR_4:
case TEST_ERASE_INJECTOR_5:
return g_test_erase_injector[eraser->block_erase - TEST_ERASE_INJECTOR_1];
#endif
/* default: total function, 0 indicates no erase function set.
* We explicitly do not want a default catch-all case in the switch
* to ensure unhandled enum's are compiler warnings.
*/
case NO_BLOCK_ERASE_FUNC: return NULL;
};
return NULL;
}
int check_block_eraser(const struct flashctx *flash, int k, int log)
{
struct block_eraser eraser = flash->chip->block_erasers[k];
if (eraser.block_erase == NO_BLOCK_ERASE_FUNC && !eraser.eraseblocks[0].count) {
if (log)
msg_cdbg("not defined. ");
return 1;
}
if (eraser.block_erase == NO_BLOCK_ERASE_FUNC && eraser.eraseblocks[0].count) {
if (log)
msg_cdbg("eraseblock layout is known, but matching "
"block erase function is not implemented. ");
return 1;
}
if (eraser.block_erase != NO_BLOCK_ERASE_FUNC && !eraser.eraseblocks[0].count) {
if (log)
msg_cdbg("block erase function found, but "
"eraseblock layout is not defined. ");
return 1;
}
if (flash->mst->buses_supported & BUS_SPI) {
const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase);
if (opcode)
for (int i = 0; opcode[i]; i++) {
if (!spi_probe_opcode(flash, opcode[i])) {
if (log)
msg_cdbg("block erase function and layout found "
"but SPI master doesn't support the function. ");
return 1;
}
}
}
// TODO: Once erase functions are annotated with allowed buses, check that as well.
return 0;
}
/* Returns the number of well-defined erasers for a chip. */
unsigned int count_usable_erasers(const struct flashctx *flash)
{
unsigned int usable_erasefunctions = 0;
int k;
for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
if (!check_block_eraser(flash, k, 0))
usable_erasefunctions++;
}
return usable_erasefunctions;
}
static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
{
int ret = 0, failcount = 0;
unsigned int i;
for (i = 0; i < len; i++) {
if (wantbuf[i] != havebuf[i]) {
/* Only print the first failure. */
if (!failcount++)
msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
start + i, wantbuf[i], havebuf[i]);
}
}
if (failcount) {
msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
start, start + len - 1, failcount);
ret = -1;
}
return ret;
}
/* start is an offset to the base address of the flash chip */
int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
{
int ret;
const uint8_t erased_value = ERASED_VALUE(flash);
uint8_t *cmpbuf = malloc(len);
if (!cmpbuf) {
msg_gerr("Out of memory!\n");
return -1;
}
memset(cmpbuf, erased_value, len);
ret = verify_range(flash, cmpbuf, start, len);
free(cmpbuf);
return ret;
}
#ifdef FLASHROM_TEST
/* special unit-test hook */
read_func_t *g_test_read_injector;
#endif
static read_func_t *lookup_read_func_ptr(const struct flashchip *chip)
{
switch (chip->read) {
case SPI_CHIP_READ: return &spi_chip_read;
case READ_OPAQUE: return &read_opaque;
case READ_MEMMAPPED: return &read_memmapped;
case EDI_CHIP_READ: return &edi_chip_read;
case SPI_READ_AT45DB: return spi_read_at45db;
case SPI_READ_AT45DB_E8: return spi_read_at45db_e8;
#ifdef FLASHROM_TEST
case TEST_READ_INJECTOR: return g_test_read_injector;
#endif
/* default: total function, 0 indicates no read function set.
* We explicitly do not want a default catch-all case in the switch
* to ensure unhandled enum's are compiler warnings.
*/
case NO_READ_FUNC: return NULL;
};
return NULL;
}
/*
* @brief Wrapper for flash->read() with additional high-level policy.
*
* @param flash flash chip
* @param buf buffer to store data in
* @param start start address
* @param len number of bytes to read
* @return 0 on success,
* -1 if any read fails.
*
* This wrapper simplifies most cases when the flash chip needs to be read
* since policy decisions such as non-fatal error handling is centralized.
*/
int read_flash(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
{
unsigned int read_len;
for (unsigned int addr = start; addr < start + len; addr += read_len) {
struct flash_region region;
get_flash_region(flash, addr, ®ion);
read_len = min(start + len, region.end + 1) - addr;
uint8_t *rbuf = buf + addr - start;
if (region.read_prot) {
if (flash->flags.skip_unreadable_regions) {
msg_gdbg("%s: cannot read inside %s region (%#08"PRIx32"..%#08"PRIx32"), "
"filling (%#08x..%#08x) with erased value instead.\n",
__func__, region.name, region.start, region.end,
addr, addr + read_len - 1);
free(region.name);
memset(rbuf, ERASED_VALUE(flash), read_len);
continue;
}
msg_gerr("%s: cannot read inside %s region (%#08"PRIx32"..%#08"PRIx32").\n",
__func__, region.name, region.start, region.end);
free(region.name);
return -1;
}
msg_gdbg("%s: %s region (%#08"PRIx32"..%#08"PRIx32") is readable, reading range (%#08x..%#08x).\n",
__func__, region.name, region.start, region.end, addr, addr + read_len - 1);
free(region.name);
read_func_t *read_func = lookup_read_func_ptr(flash->chip);
int ret = read_func(flash, rbuf, addr, read_len);
if (ret) {
msg_gerr("%s: failed to read (%#08x..%#08x).\n", __func__, addr, addr + read_len - 1);
return -1;
}
}
return 0;
}
/*
* @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
* flash content at location start
* @start offset to the base address of the flash chip
* @len length of the verified area
* @return 0 for success, -1 for failure
*/
int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
{
if (!len)
return -1;
if (start + len > flash->chip->total_size * 1024) {
msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
" total_size 0x%x\n", __func__, start, len,
flash->chip->total_size * 1024);
return -1;
}
uint8_t *readbuf = malloc(len);
if (!readbuf) {
msg_gerr("Out of memory!\n");
return -1;
}
int ret = 0;
msg_gdbg("%#06x..%#06x ", start, start + len - 1);
unsigned int read_len;
for (size_t addr = start; addr < start + len; addr += read_len) {
struct flash_region region;
get_flash_region(flash, addr, ®ion);
read_len = min(start + len, region.end + 1) - addr;
if ((region.write_prot && flash->flags.skip_unwritable_regions) ||
(region.read_prot && flash->flags.skip_unreadable_regions)) {
msg_gdbg("%s: Skipping verification of %s region (%#08"PRIx32"..%#08"PRIx32")\n",
__func__, region.name, region.start, region.end);
free(region.name);
continue;
}
if (region.read_prot) {
msg_gerr("%s: Verification imposible because %s region (%#08"PRIx32"..%#08"PRIx32") is unreadable.\n",
__func__, region.name, region.start, region.end);
free(region.name);
goto out_free;
}
msg_gdbg("%s: Verifying %s region (%#08"PRIx32"..%#08"PRIx32")\n",
__func__, region.name, region.start, region.end);
free(region.name);
ret = read_flash(flash, readbuf, addr, read_len);
if (ret) {
msg_gerr("Verification impossible because read failed "
"at 0x%x (len 0x%x)\n", start, len);
ret = -1;
goto out_free;
}
ret = compare_range(cmpbuf + (addr - start), readbuf, addr, read_len);
if (ret)
goto out_free;
}
out_free:
free(readbuf);
return ret;
}
/* Helper function for need_erase() that focuses on granularities of gran bytes. */
static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
unsigned int gran, const uint8_t erased_value)
{
unsigned int i, j, limit;
for (j = 0; j < len / gran; j++) {
limit = min (gran, len - j * gran);
/* Are 'have' and 'want' identical? */
if (!memcmp(have + j * gran, want + j * gran, limit))
continue;
/* have needs to be in erased state. */
for (i = 0; i < limit; i++)
if (have[j * gran + i] != erased_value)
return 1;
}
return 0;
}
/*
* Check if the buffer @have can be programmed to the content of @want without
* erasing. This is only possible if all chunks of size @gran are either kept
* as-is or changed from an all-ones state to any other state.
*
* Warning: This function assumes that @have and @want point to naturally
* aligned regions.
*
* @have buffer with current content
* @want buffer with desired content
* @len length of the checked area
* @gran write granularity (enum, not count)
* @return 0 if no erase is needed, 1 otherwise
*/
int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
enum write_granularity gran, const uint8_t erased_value)
{
int result = 0;
unsigned int i;
switch (gran) {
case WRITE_GRAN_1BIT:
for (i = 0; i < len; i++)
if ((have[i] & want[i]) != want[i]) {
result = 1;
break;
}
break;
case WRITE_GRAN_1BYTE:
for (i = 0; i < len; i++)
if ((have[i] != want[i]) && (have[i] != erased_value)) {
result = 1;
break;
}
break;
case WRITE_GRAN_128BYTES:
result = need_erase_gran_bytes(have, want, len, 128, erased_value);
break;
case WRITE_GRAN_256BYTES:
result = need_erase_gran_bytes(have, want, len, 256, erased_value);
break;
case WRITE_GRAN_264BYTES:
result = need_erase_gran_bytes(have, want, len, 264, erased_value);
break;
case WRITE_GRAN_512BYTES:
result = need_erase_gran_bytes(have, want, len, 512, erased_value);
break;
case WRITE_GRAN_528BYTES:
result = need_erase_gran_bytes(have, want, len, 528, erased_value);
break;
case WRITE_GRAN_1024BYTES:
result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
break;
case WRITE_GRAN_1056BYTES:
result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
break;
case WRITE_GRAN_1BYTE_IMPLICIT_ERASE:
/* Do not erase, handle content changes from anything->0xff by writing 0xff. */
result = 0;
break;
default:
msg_cerr("%s: Unsupported granularity! Please report a bug at "
"[email protected]\n", __func__);
}
return result;
}
/**
* Check if the buffer @have needs to be programmed to get the content of @want.
* If yes, return 1 and fill in first_start with the start address of the
* write operation and first_len with the length of the first to-be-written
* chunk. If not, return 0 and leave first_start and first_len undefined.
*
* Warning: This function assumes that @have and @want point to naturally
* aligned regions.
*
* @have buffer with current content
* @want buffer with desired content
* @len length of the checked area
* @gran write granularity (enum, not count)
* @first_start offset of the first byte which needs to be written (passed in
* value is increased by the offset of the first needed write
* relative to have/want or unchanged if no write is needed)
* @return length of the first contiguous area which needs to be written
* 0 if no write is needed
*
* FIXME: This function needs a parameter which tells it about coalescing
* in relation to the max write length of the programmer and the max write
* length of the chip.
*/
unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
unsigned int *first_start,
enum write_granularity gran)
{
bool need_write = false;
unsigned int rel_start = 0, first_len = 0;
unsigned int i, limit, stride;
switch (gran) {
case WRITE_GRAN_1BIT:
case WRITE_GRAN_1BYTE:
case WRITE_GRAN_1BYTE_IMPLICIT_ERASE:
stride = 1;
break;
case WRITE_GRAN_128BYTES:
stride = 128;
break;
case WRITE_GRAN_256BYTES:
stride = 256;
break;
case WRITE_GRAN_264BYTES:
stride = 264;
break;
case WRITE_GRAN_512BYTES:
stride = 512;
break;
case WRITE_GRAN_528BYTES:
stride = 528;
break;
case WRITE_GRAN_1024BYTES:
stride = 1024;
break;
case WRITE_GRAN_1056BYTES:
stride = 1056;
break;
default:
msg_cerr("%s: Unsupported granularity! Please report a bug at "
"[email protected]\n", __func__);
/* Claim that no write was needed. A write with unknown
* granularity is too dangerous to try.
*/
return 0;
}
for (i = 0; i < len / stride; i++) {
limit = min(stride, len - i * stride);
/* Are 'have' and 'want' identical? */
if (memcmp(have + i * stride, want + i * stride, limit)) {
if (!need_write) {
/* First location where have and want differ. */
need_write = true;
rel_start = i * stride;
}
} else {
if (need_write) {
/* First location where have and want
* do not differ anymore.
*/
break;
}
}
}
if (need_write)
first_len = min(i * stride - rel_start, len);
*first_start += rel_start;
return first_len;
}
void unmap_flash(struct flashctx *flash)
{
if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
master_unmap_flash_region(flash->mst, (void *)flash->virtual_registers, flash->chip->total_size * 1024);
flash->physical_registers = 0;
flash->virtual_registers = (chipaddr)ERROR_PTR;
}
if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
master_unmap_flash_region(flash->mst, (void *)flash->virtual_memory, flash->chip->total_size * 1024);
flash->physical_memory = 0;
flash->virtual_memory = (chipaddr)ERROR_PTR;
}
}
int map_flash(struct flashctx *flash)
{
/* Init pointers to the fail-safe state to distinguish them later from legit values. */
flash->virtual_memory = (chipaddr)ERROR_PTR;
flash->virtual_registers = (chipaddr)ERROR_PTR;
/* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
* These are used for various probing-related hacks that would not map successfully anyway and should be
* removed ASAP. */
if (flash->chip->total_size == 0)
return 0;
const chipsize_t size = flash->chip->total_size * 1024;
uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
void *addr = master_map_flash_region(flash->mst, flash->chip->name, base, size);
if (addr == ERROR_PTR) {
msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
flash->chip->name, PRIxPTR_WIDTH, base);
return 1;
}
flash->physical_memory = base;
flash->virtual_memory = (chipaddr)addr;
/* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
* completely different on some chips and programmers, or not mappable at all.
* Ignore these problems for now and always report success. */
if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
base = 0xffffffff - size - 0x400000 + 1;
addr = master_map_flash_region(flash->mst, "flash chip registers", base, size);
if (addr == ERROR_PTR) {
msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
flash->chip->name, PRIxPTR_WIDTH, base);
return 0;
}
flash->physical_registers = base;
flash->virtual_registers = (chipaddr)addr;
}
return 0;
}
/*
* Return a string corresponding to the bustype parameter.
* Memory to store the string is allocated. The caller is responsible to free memory.
* If there is not enough memory remaining, then NULL is returned.
*/
char *flashbuses_to_text(enum chipbustype bustype)
{
char *ret, *ptr;
/*
* FIXME: Once all chipsets and flash chips have been updated, NONSPI
* will cease to exist and should be eliminated here as well.
*/
if (bustype == BUS_NONSPI)
return strdup("Non-SPI");
if (bustype == BUS_NONE)
return strdup("None");
ret = calloc(1, 1);
if (!ret)
return NULL;
for (unsigned int i = 0; i < ARRAY_SIZE(bustypes); i++)
{
if (bustype & bustypes[i].type) {
ptr = strcat_realloc(ret, bustypes[i].name);
if (!ptr) {
free(ret);
return NULL;
}
ret = ptr;
}
}
/* Kill last comma. */
ret[strlen(ret) - 2] = '\0';
ptr = realloc(ret, strlen(ret) + 1);
if (!ptr)
free(ret);
return ptr;
}
static int init_default_layout(struct flashctx *flash)
{
/* Fill default layout covering the whole chip. */
if (flashrom_layout_new(&flash->default_layout) ||
flashrom_layout_add_region(flash->default_layout,
0, flash->chip->total_size * 1024 - 1, "complete flash") ||
flashrom_layout_include_region(flash->default_layout, "complete flash"))
return -1;
return 0;
}
/* special unit-test hook */
#ifdef FLASHROM_TEST
write_func_t *g_test_write_injector;
#endif
static write_func_t *lookup_write_func_ptr(const struct flashchip *chip)
{
switch (chip->write) {
case WRITE_JEDEC: return &write_jedec;
case WRITE_JEDEC1: return &write_jedec_1;
case WRITE_OPAQUE: return &write_opaque;