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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

use byteorder::{LittleEndian, ReadBytesExt};
use num_enum::FromPrimitive;
use serde::Serialize;
use serde_annotate::Annotate;
use std::convert::TryFrom;
use std::time::Duration;
use thiserror::Error;

use crate::util::bitfield::BitField;

#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    TryFromIntError(#[from] std::num::TryFromIntError),
    #[error("the range {0}..{1} is out of bounds")]
    SliceRange(usize, usize),
    #[error("SFDP header contains incorrect signature: {0:#010x}")]
    WrongHeaderSignature(u32),

    // This is only needed to meet the error conversion requirements for the most
    // general case in the field! macro below.
    #[error(transparent)]
    Infallible(#[from] std::convert::Infallible),
}

/// The SFDP header identifies a valid SFDP, its version and the number of parameter headers.
#[derive(Debug, Serialize, Annotate)]
pub struct SfdpHeader {
    #[annotate(format=hex, comment=comment_signature())]
    pub signature: u32,
    #[annotate(comment=comment_version())]
    pub minor: u8,
    pub major: u8,
    #[annotate(comment = "Number of parameter headers minus one")]
    pub nph: u8,
    #[annotate(format=bin, comment="The reserved field should be all ones")]
    pub reserved: u8,
}

impl SfdpHeader {
    fn comment_signature(&self) -> Option<String> {
        let signature = self.signature.to_le_bytes().map(|b| {
            match b {
                // The ASCII printable range is [0x20, 0x7E].
                0x20..=0x7E => b as char,
                _ => '.',
            }
        });
        Some(format!(
            "Signature value='{}{}{}{}' (should be 'SFDP')",
            signature[0], signature[1], signature[2], signature[3],
        ))
    }
    fn comment_version(&self) -> Option<String> {
        Some(format!("{}.{}", self.major, self.minor))
    }
}

impl TryFrom<&[u8]> for SfdpHeader {
    type Error = Error;
    fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
        const SFDP_HEADER_SIGNATURE: u32 = 0x50444653;

        let mut reader = std::io::Cursor::new(buf);
        let header = SfdpHeader {
            signature: reader.read_u32::<LittleEndian>()?,
            minor: reader.read_u8()?,
            major: reader.read_u8()?,
            nph: reader.read_u8()?,
            reserved: reader.read_u8()?,
        };
        match header.signature {
            SFDP_HEADER_SIGNATURE => Ok(header),
            v => Err(Error::WrongHeaderSignature(v)),
        }
    }
}

/// The SFDP parameter header identifies additional parameter tables within the SFDP.
/// All devices are required to have a JEDEC parameter header and corresponding table.
#[derive(Debug, Serialize)]
pub struct SfdpPhdr {
    pub id: u8,
    pub minor: u8,
    pub major: u8,
    pub dwords: u8,
    pub offset: u32,
}

impl TryFrom<&[u8]> for SfdpPhdr {
    type Error = Error;
    fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
        let mut reader = std::io::Cursor::new(buf);
        Ok(SfdpPhdr {
            id: reader.read_u8()?,
            minor: reader.read_u8()?,
            major: reader.read_u8()?,
            dwords: reader.read_u8()?,
            offset: reader.read_u32::<LittleEndian>()? & 0x00FFFFFFu32,
        })
    }
}

// The internal JEDEC parameter struct is used as an intermediate representation
// in creating the user-visible JEDEC parameter struct.
struct InternalJedecParams {
    pub data: Vec<u32>,
}

impl TryFrom<&[u8]> for InternalJedecParams {
    type Error = Error;
    fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
        let mut reader = std::io::Cursor::new(buf);
        let mut data = vec![0u32; buf.len() / 4];
        reader.read_u32_into::<LittleEndian>(&mut data)?;
        Ok(InternalJedecParams { data })
    }
}

// The JEDEC parameter table is documented in JESD216 "Serial Flash Discoverable Parameters".
// The format of the parameter table is expressed in that document as a set of bitfields
// in the various "dwords" which make up the table.
//
// We use the `field` macro to express how to extract and convert the various bitfields
// into useful values.  The macro generates a function named `ident` which extracts
// `bitsize` bits starting at `bitoffset` in the `field`th "dword" of the data.
//
// Since the documentation for dword offsets is 1-based, the marco parameter `field`
// is also 1-based.
macro_rules! field {
    // Extract to bool.
    ($name:ident -> bool, $field:expr, $bitoffset:expr, $bitsize:expr) => {
        pub fn $name(&self) -> Result<bool, Error> {
            Ok(BitField::new($bitoffset, $bitsize).extract(self.data[$field - 1]) != 0)
        }
    };
    // Extract an entire u32 word.
    ($name:ident -> u32, $field:expr, 0, 32) => {
        pub fn $name(&self) -> Result<u32, Error> {
            Ok(self.data[$field - 1])
        }
    };
    // Extract a bitfield as u32.
    ($name:ident -> u32, $field:expr, $bitoffset:expr, $bitsize:expr) => {
        pub fn $name(&self) -> Result<u32, Error> {
            Ok(BitField::new($bitoffset, $bitsize).extract(self.data[$field - 1]))
        }
    };
    // Extract a bitfield as some other type.
    ($name:ident -> $type:ty, $field:expr, $bitoffset:expr, $bitsize:expr) => {
        pub fn $name(&self) -> Result<$type, Error> {
            Ok(<$type>::try_from(
                BitField::new($bitoffset, $bitsize).extract(self.data[$field - 1]),
            )?)
        }
    };
}

impl InternalJedecParams {
    field!(block_erase_size -> BlockEraseSize, 1, 0, 2);
    field!(write_granularity -> WriteGranularity, 1, 2, 1);
    field!(write_en_required -> bool, 1, 3, 1);
    field!(write_en_opcode -> bool, 1, 4, 1);
    field!(erase_opcode_4kib -> u8, 1, 8, 8);
    field!(support_fast_read_112 -> bool, 1, 16, 1);
    field!(address_modes -> SupportedAddressModes, 1, 17, 2);
    field!(support_double_rate_clocking -> bool, 1, 19, 1);
    field!(support_fast_read_122 -> bool, 1, 20, 1);
    field!(support_fast_read_144 -> bool, 1, 21, 1);
    field!(support_fast_read_114 -> bool, 1, 22, 1);

    field!(density -> u32, 2, 0, 31);
    field!(density_pow2 -> bool, 2, 31, 1);

    field!(wait_states_144 -> u8, 3, 0, 5);
    field!(mode_bits_144 -> u8, 3, 5, 3);
    field!(read_opcode_144 -> u8, 3, 8, 8);
    field!(wait_states_114 -> u8, 3, 16, 5);
    field!(mode_bits_114 -> u8, 3, 21, 3);
    field!(read_opcode_114 -> u8, 3, 24, 8);

    field!(wait_states_112 -> u8, 4, 0, 5);
    field!(mode_bits_112 -> u8, 4, 5, 3);
    field!(read_opcode_112 -> u8, 4, 8, 8);
    field!(wait_states_122 -> u8, 4, 16, 5);
    field!(mode_bits_122 -> u8, 4, 21, 3);
    field!(read_opcode_122 -> u8, 4, 24, 8);

    field!(support_fast_read_222 -> bool, 5, 0, 1);
    field!(support_fast_read_444 -> bool, 5, 4, 1);

    field!(wait_states_222 -> u8, 6, 16, 5);
    field!(mode_bits_222 -> u8, 6, 21, 3);
    field!(read_opcode_222 -> u8, 6, 24, 8);

    field!(wait_states_444 -> u8, 7, 16, 5);
    field!(mode_bits_444 -> u8, 7, 21, 3);
    field!(read_opcode_444 -> u8, 7, 24, 8);

    field!(sector_type1_size -> u8, 8, 0, 8);
    field!(sector_type1_opcode -> u8, 8, 8, 8);
    field!(sector_type2_size -> u8, 8, 16, 8);
    field!(sector_type2_opcode -> u8, 8, 24, 8);

    field!(sector_type3_size -> u8, 9, 0, 8);
    field!(sector_type3_opcode -> u8, 9, 8, 8);
    field!(sector_type4_size -> u8, 9, 16, 8);
    field!(sector_type4_opcode -> u8, 9, 24, 8);

    // JESD216B pp. 20-21.
    field!(erase_time_multiplier -> u32, 10, 0, 4);
    field!(erase_type1_time -> u32, 10, 4, 5);
    field!(erase_type1_unit -> usize, 10, 9, 2);
    field!(erase_type2_time -> u32, 10, 11, 5);
    field!(erase_type2_unit -> usize, 10, 16, 2);
    field!(erase_type3_time -> u32, 10, 18, 5);
    field!(erase_type3_unit -> usize, 10, 23, 2);
    field!(erase_type4_time -> u32, 10, 25, 5);
    field!(erase_type4_unit -> usize, 10, 30, 2);

    // JESD216B pg. 22.
    field!(pgm_time_multiplier -> u32, 11, 0, 4);
    field!(page_size -> u32, 11, 4, 4);
    field!(page_pgm_time -> u32, 11, 8, 5);
    field!(page_pgm_unit -> usize, 11, 13, 1);
    field!(byte_pgm_time -> u32, 11, 14, 4);
    field!(byte_pgm_unit -> usize, 11, 18, 1);
    field!(additional_byte_pgm_time -> u32, 11, 19, 4);
    field!(additional_byte_pgm_unit -> usize, 11, 23, 1);
    field!(chip_erase_time -> u32, 11, 24, 5);
    field!(chip_erase_unit -> usize, 11, 29, 2);

    // JESD216B pp. 23-24.
    field!(prohibited_pgm_suspend -> u8, 12, 0, 4);
    field!(prohibited_erase_suspend -> u8, 12, 4, 4);
    field!(pgm_resume_to_suspend -> u32, 12, 9, 4);
    field!(suspend_pgm_latency_time -> u32, 12, 13, 5);
    field!(suspend_pgm_latency_unit -> usize, 12, 18, 2);
    field!(erase_resume_to_suspend -> u32, 12, 20, 4);
    field!(suspend_erase_latency_time -> u32, 12, 24, 5);
    field!(suspend_erase_latency_unit -> usize, 12, 29, 2);
    field!(suspend_resume_supported -> bool, 12, 31, 1);

    // JESD216B pg. 25.
    field!(pgm_resume_instruction -> u8, 13, 0, 8);
    field!(pgm_suspend_instruction -> u8, 13, 8, 8);
    field!(resume_instruction -> u8, 13, 16, 8);
    field!(suspend_instruction -> u8, 13, 24, 8);

    // JESD216B pg. 25.
    field!(status_busy_polling -> u8, 14, 2, 6);
    field!(exit_deep_powerdown_time -> u32, 14, 8, 5);
    field!(exit_deep_powerdown_unit -> usize, 14, 13, 2);
    field!(exit_deep_powerdown_instruction -> u8, 14, 15, 8);
    field!(enter_deep_powerdown_instruction -> u8, 14, 23, 8);
    field!(deep_powerdown_supported -> bool, 14, 31, 1);

    // JESD216B pp. 26-27.
    field!(mode_444_disable -> u8, 15, 0, 4);
    field!(mode_444_enable -> u8, 15, 4, 5);
    field!(mode_444_supported -> bool, 15, 9, 1);
    field!(mode_444_exit -> u8, 15, 10, 6);
    field!(mode_444_entry -> u8, 15, 16, 4);
    field!(quad_enable_requirements -> u8, 15, 20, 3);
    field!(hold_or_reset_disable -> bool, 15, 23, 1);

    // JESD216B pp. 28-29.
    field!(status_reg1_write_enable -> u8, 16, 0, 6);
    field!(soft_reset_support -> u8, 16, 8, 6);
    field!(exit_4b_addressing -> u16, 16, 14, 10);
    field!(enter_4b_addressing -> u8, 16, 24, 8);

    // JESD216D.01 pg. 44.
    field!(wait_states_188 -> u8, 17, 0, 5);
    field!(mode_bits_188 -> u8, 17, 5, 3);
    field!(read_opcode_188 -> u8, 17, 8, 8);
    field!(wait_states_118 -> u8, 17, 16, 5);
    field!(mode_bits_118 -> u8, 17, 21, 3);
    field!(read_opcode_118 -> u8, 17, 24, 8);

    // JESD216D.01 pg. 45.
    field!(output_driver_strength -> u8, 18, 18, 5);
    field!(spi_protocol_reset -> bool, 18, 23, 1);
    field!(data_strobe_str -> u8, 18, 24, 2);
    field!(data_qpi_str -> bool, 18, 26, 1);
    field!(data_qpi_dtr -> bool, 18, 27, 1);
    field!(octal_dtr_command -> u8, 18, 29, 2);
    field!(octal_byte_order -> u8, 18, 31, 1);

    // JESD216D.01 pg. 48.
    field!(mode_8s8s8s_disable-> u8, 19, 0, 4);
    field!(mode_8s8s8s_enable-> u8, 19, 4, 5);
    field!(mode_088_supported -> bool, 19, 9, 1);
    field!(mode_088_exit -> u8, 19, 10, 6);
    field!(mode_088_enter -> u8, 19, 16, 4);
    field!(octal_enable_requirements -> u8, 19, 20, 3);

    // JESD216D.01 pp. 49-51.
    field!(max_speed_4s4s4s_no_strobe -> MaxSpeed, 20, 0, 4);
    field!(max_speed_4s4s4s_strobe    -> MaxSpeed, 20, 4, 4);
    field!(max_speed_4d4d4d_no_strobe -> MaxSpeed, 20, 8, 4);
    field!(max_speed_4d4d4d_strobe    -> MaxSpeed, 20, 12, 4);
    field!(max_speed_8s8s8s_no_strobe -> MaxSpeed, 20, 16, 4);
    field!(max_speed_8s8s8s_strobe    -> MaxSpeed, 20, 20, 4);
    field!(max_speed_8d8d8d_no_strobe -> MaxSpeed, 20, 24, 4);
    field!(max_speed_8d8d8d_strobe    -> MaxSpeed, 20, 28, 4);

    // JESD216F.01 pg. 52.
    field!(support_fast_read_1s1d1d -> bool, 21, 0, 1);
    field!(support_fast_read_1s2d2d -> bool, 21, 1, 1);
    field!(support_fast_read_1s4d4d -> bool, 21, 2, 1);
    field!(support_fast_read_4s4d4d -> bool, 21, 3, 1);

    // JESD216F.01 pg. 53.
    field!(wait_states_1s1d1d -> u8, 22, 0, 5);
    field!(mode_bits_1s1d1d   -> u8, 22, 5, 3);
    field!(read_opcode_1s1d1d -> u8, 22, 8, 8);
    field!(wait_states_1s2d2d -> u8, 22, 16, 5);
    field!(mode_bits_1s2d2d   -> u8, 22, 21, 3);
    field!(read_opcode_1s2d2d -> u8, 22, 24, 8);

    // JESD216F.01 pg. 54.
    field!(wait_states_1s4d4d -> u8, 23, 0, 5);
    field!(mode_bits_1s4d4d   -> u8, 23, 5, 3);
    field!(read_opcode_1s4d4d -> u8, 23, 8, 8);
    field!(wait_states_4s4d4d -> u8, 23, 16, 5);
    field!(mode_bits_4s4d4d   -> u8, 23, 21, 3);
    field!(read_opcode_4s4d4d -> u8, 23, 24, 8);
}

/// `BlockEraseSize` represents whether or not the device can perform
/// a 4KiB erase.
#[derive(Default, Debug, Eq, PartialEq, FromPrimitive, Clone, Copy, Serialize)]
#[repr(u32)]
pub enum BlockEraseSize {
    Reserved0 = 0,
    Block4KiB = 1,
    Reserved2 = 2,
    BlockNot4KiB = 3,
    #[default]
    Invalid,
}

/// `WriteGranularity` represents whether or not the device has an internal
/// buffer for program operations.
#[derive(Default, Debug, Eq, PartialEq, FromPrimitive, Clone, Copy, Serialize)]
#[repr(u32)]
pub enum WriteGranularity {
    Granularity1Byte = 0,
    Granularity64Bytes = 1,
    #[default]
    Invalid,
}

/// `SupportedAddressModes` represents which addressing modes are valid for
/// the device.
#[derive(Default, Debug, Eq, PartialEq, FromPrimitive, Clone, Copy, Serialize)]
#[repr(u32)]
pub enum SupportedAddressModes {
    Mode3b = 0,
    Mode3b4b = 1,
    Mode4b = 2,
    Reserved = 3,
    #[default]
    Invalid,
}

#[derive(Default, Debug, Eq, PartialEq, FromPrimitive, Clone, Copy, Serialize)]
#[repr(u32)]
pub enum MaxSpeed {
    Reserved0 = 0,
    Speed33MHz,
    Speed50MHz,
    Speed66MHz,
    Speed80MHz,
    Speed100MHz,
    Speed133MHz,
    Speed166MHz,
    Speed200MHz,
    Speed250MHz,
    Speed266MHz,
    Speed333MHz,
    Speed400MHz,
    Reserved10,
    NotCharacterized,
    #[default]
    NotSupported,
}

/// `FastReadParam` represents the parameters for the different styles of fast read.
#[derive(Clone, Default, Debug, Serialize, Annotate)]
pub struct FastReadParam {
    pub wait_states: u8,
    pub mode_bits: u8,
    #[annotate(format=hex)]
    pub opcode: u8,
}

/// `SectorErase` represents the supported erase sector sizes of the device.
#[derive(Clone, Default, Debug, Serialize, Annotate)]
pub struct SectorErase {
    pub size: u32,
    #[annotate(format=hex)]
    pub opcode: u8,
    pub time: Option<TimeBound>,
}

#[derive(Clone, Default, Debug, Serialize)]
pub struct TimeBound {
    #[serde(with = "humantime_serde")]
    pub typical: Duration,
    #[serde(with = "humantime_serde")]
    pub maximum: Duration,
}

/// The JEDEC parameter table is documented in JESD216 "Serial Flash Discoverable Parameters".
/// This structure represents the parameter table after decoding it from the packed bitfield
/// representation documented by JEDEC.
///
/// I have access to the following specs:
/// - JESD216 dated April 2011 (Flash parameters version 1.0).
/// - JESD216B dated May 2014 (Flash parameters version 1.6).
/// - JESD216D dated August 2019 (Flash parameters version 1.7, unchanged since rev C).
/// - JESD216F dated February 2022 (Flash parameters version 1.7, unchanged since rev D).
///
/// - Version 1.0 defines a table of 9 "dwords". I'm considering this to be the
///   minimum required parameter table.
/// - Rev B, version 1.6 extends the table to 16 "dwords", including information about
///   erase timing, powerdown and alternate mode requirements.
/// - Rev D, version 1.7 extends the table to 20 "dwords", including information about
///   eight-lane SPI modes.
/// - Rev F, version 1.7 extends the table to 23 "dwords", including information about
///   dual data rate operations.
#[derive(Default, Debug, Serialize, Annotate)]
pub struct JedecParams {
    /// Erase granularity.
    pub block_erase_size: BlockEraseSize,
    /// Write granularity / buffer size.
    pub write_granularity: WriteGranularity,
    /// WREN instruction required for writing to volatile status register.
    pub write_en_required: bool,
    /// Write enable opocde (this is a single bit in the jedec table; expanded
    /// to the actual opcode here).
    #[annotate(format=hex)]
    pub write_en_opcode: u8,
    /// Erase opcode for 4KiB sector erase.
    #[annotate(format=hex)]
    pub erase_opcode_4kib: u8,
    /// Support Fast Read 1-1-2.
    pub support_fast_read_112: bool,
    /// The address modes supported by the device.
    pub address_modes: SupportedAddressModes,
    /// Device supports double rate clocking.
    pub support_double_rate_clocking: bool,
    /// Other styles of Fast Read.  The numerical designator refers to
    /// the instruction transfer mode, the address transfer mode and data
    /// transfer mode.
    /// ie: 1-2-2 means single bit mode for the opcode, dual mode for the address
    /// and dual mode for the data phase.
    pub support_fast_read_122: bool,
    pub support_fast_read_144: bool,
    pub support_fast_read_114: bool,
    pub support_fast_read_222: bool,
    pub support_fast_read_444: bool,

    /// The density of the part. In the jedec table, this is expressed in bits,
    /// but it is converted to bytes here.
    pub density: u32,
    /// Parameters for the various supported FastRead modes.
    pub param_112: FastReadParam,
    pub param_122: FastReadParam,
    pub param_114: FastReadParam,
    pub param_144: FastReadParam,
    pub param_222: FastReadParam,
    pub param_444: FastReadParam,
    /// Erase sector sizes.  It is common for devices to support a 4KiB erase
    /// size, a 32KiB erase size and a 64KiB erase size.
    pub erase: [SectorErase; 4],

    // TODO: Get ahold of Revs A, C & E and restructure the revision extensions.
    pub rev_b: Option<JedecParamsRevB>,
    pub rev_d: Option<JedecParamsRevD>,
    pub rev_f: Option<JedecParamsRevF>,
}

/// The Rev B extensions to the JEDEC parameters table.
#[derive(Default, Debug, Serialize, Annotate)]
pub struct JedecParamsRevB {
    pub page_size: u32,
    pub page_program_time: TimeBound,
    pub byte_program_time: TimeBound,
    pub additional_byte_program_time: TimeBound,
    pub chip_erase_time: TimeBound,

    pub suspend_resume_supported: bool,
    pub prohibited_ops_program_suspend: u8,
    pub prohibited_ops_erase_suspend: u8,
    #[serde(with = "humantime_serde")]
    pub program_resume_to_suspend: Duration,
    #[serde(with = "humantime_serde")]
    pub suspend_in_progress_program_latency: Duration,
    #[serde(with = "humantime_serde")]
    pub erase_resume_to_suspend: Duration,
    #[serde(with = "humantime_serde")]
    pub suspend_in_progress_erase_latency: Duration,
    #[annotate(format=hex)]
    pub program_resume_instruction: u8,
    #[annotate(format=hex)]
    pub program_suspend_instruction: u8,
    #[annotate(format=hex)]
    pub resume_instruction: u8,
    #[annotate(format=hex)]
    pub suspend_instruction: u8,

    pub deep_powerdown_supported: bool,
    #[annotate(format=hex)]
    pub enter_deep_powerdown_instruction: u8,
    #[annotate(format=hex)]
    pub exit_deep_powerdown_instruction: u8,
    #[serde(with = "humantime_serde")]
    pub exit_deep_powerdown_delay: Duration,
    pub status_register_polling: u8,

    pub hold_or_reset_disable: bool,
    pub quad_enable_requirements: u8,
    pub mode_444_entry: u8,
    pub mode_444_exit: u8,
    pub mode_444_supported: bool,
    pub mode_444_disable: u8,
    pub mode_444_enable: u8,

    pub enter_4b_addressing: u8,
    pub exit_4b_addressing: u16,
    pub soft_reset_support: u8,
    pub status_reg1_write_enable: u8,
}

/// The Rev D extensions to the JEDEC parameters table.
#[derive(Default, Debug, Serialize)]
pub struct JedecParamsRevD {
    pub param_118: FastReadParam,
    pub param_188: FastReadParam,

    pub output_driver_strength: u8,
    pub jedec_spi_protocol_reset: bool,
    pub data_strobe_waveform_str: u8,
    pub data_strobe_support_4s4s4s: bool,
    pub data_strobe_support_4d4d4d: bool,
    pub octal_dtr_command: u8,
    pub octal_byte_order: u8,

    pub mode_8s8s8s_disable: u8,
    pub mode_8s8s8s_enable: u8,
    pub mode_088_supported: bool,
    pub mode_088_exit: u8,
    pub mode_088_enter: u8,
    pub octal_enable_requirements: u8,

    pub max_speed_4s4s4s_no_strobe: MaxSpeed,
    pub max_speed_4s4s4s_strobe: MaxSpeed,
    pub max_speed_4d4d4d_no_strobe: MaxSpeed,
    pub max_speed_4d4d4d_strobe: MaxSpeed,
    pub max_speed_8s8s8s_no_strobe: MaxSpeed,
    pub max_speed_8s8s8s_strobe: MaxSpeed,
    pub max_speed_8d8d8d_no_strobe: MaxSpeed,
    pub max_speed_8d8d8d_strobe: MaxSpeed,
}

/// The Rev F extensions to the JEDEC parameters table.
#[derive(Default, Debug, Serialize)]
pub struct JedecParamsRevF {
    pub param_1s1d1d: Option<FastReadParam>,
    pub param_1s2d2d: Option<FastReadParam>,
    pub param_1s4d4d: Option<FastReadParam>,
    pub param_4s4d4d: Option<FastReadParam>,
}

impl JedecParams {
    // JESD216B section 6.4.13 (pg. 20)
    const ERASE_TIME_UNITS: [Duration; 4] = [
        Duration::from_millis(1),
        Duration::from_millis(16),
        Duration::from_millis(128),
        Duration::from_secs(1),
    ];
    // JESD216B section 6.4.14 (pg. 22)
    const CHIP_ERASE_UNITS: [Duration; 4] = [
        Duration::from_millis(16),
        Duration::from_millis(256),
        Duration::from_secs(4),
        Duration::from_secs(64),
    ];
    // JESD216B section 6.4.14 (pg. 22)
    const PAGE_PGM_UNITS: [Duration; 2] = [Duration::from_micros(8), Duration::from_micros(64)];
    // JESD216B section 6.4.14 (pg. 22)
    const BYTE_PGM_UNITS: [Duration; 2] = [Duration::from_micros(1), Duration::from_micros(8)];
    // JESD216B section 6.4.15 (pg. 23)
    const SUSPEND_RESUME_UNITS: [Duration; 4] = [
        Duration::from_nanos(128),
        Duration::from_micros(1),
        Duration::from_micros(8),
        Duration::from_micros(64),
    ];

    fn pow2_size(shift: u8) -> u32 {
        if shift == 0 {
            0
        } else {
            1u32 << shift
        }
    }
}

impl TryFrom<&[u8]> for JedecParams {
    type Error = Error;
    fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
        let p = InternalJedecParams::try_from(buf)?;
        let size_bytes = if p.density_pow2()? {
            1u32 << ((p.density()? & 0x7FFF_FFFF) - 8)
        } else {
            (p.density()? + 1) / 8
        };
        let mut jedec = JedecParams {
            block_erase_size: p.block_erase_size()?,
            write_granularity: p.write_granularity()?,
            write_en_required: p.write_en_required()?,
            write_en_opcode: if p.write_en_opcode()? { 0x50 } else { 0x06 },
            erase_opcode_4kib: p.erase_opcode_4kib()?,
            support_fast_read_112: p.support_fast_read_112()?,
            address_modes: p.address_modes()?,
            support_double_rate_clocking: p.support_double_rate_clocking()?,
            support_fast_read_122: p.support_fast_read_122()?,
            support_fast_read_144: p.support_fast_read_144()?,
            support_fast_read_114: p.support_fast_read_114()?,
            support_fast_read_222: p.support_fast_read_222()?,
            support_fast_read_444: p.support_fast_read_444()?,
            density: size_bytes,
            erase: [
                SectorErase {
                    size: Self::pow2_size(p.sector_type1_size()?),
                    opcode: p.sector_type1_opcode()?,
                    time: None,
                },
                SectorErase {
                    size: Self::pow2_size(p.sector_type2_size()?),
                    opcode: p.sector_type2_opcode()?,
                    time: None,
                },
                SectorErase {
                    size: Self::pow2_size(p.sector_type3_size()?),
                    opcode: p.sector_type3_opcode()?,
                    time: None,
                },
                SectorErase {
                    size: Self::pow2_size(p.sector_type4_size()?),
                    opcode: p.sector_type4_opcode()?,
                    time: None,
                },
            ],
            ..Default::default()
        };
        if jedec.support_fast_read_112 {
            jedec.param_112 = FastReadParam {
                wait_states: p.wait_states_112()?,
                mode_bits: p.mode_bits_112()?,
                opcode: p.read_opcode_112()?,
            }
        }
        if jedec.support_fast_read_122 {
            jedec.param_122 = FastReadParam {
                wait_states: p.wait_states_122()?,
                mode_bits: p.mode_bits_122()?,
                opcode: p.read_opcode_122()?,
            }
        }
        if jedec.support_fast_read_144 {
            jedec.param_144 = FastReadParam {
                wait_states: p.wait_states_144()?,
                mode_bits: p.mode_bits_144()?,
                opcode: p.read_opcode_144()?,
            }
        }
        if jedec.support_fast_read_114 {
            jedec.param_114 = FastReadParam {
                wait_states: p.wait_states_114()?,
                mode_bits: p.mode_bits_114()?,
                opcode: p.read_opcode_114()?,
            }
        }
        if jedec.support_fast_read_222 {
            jedec.param_222 = FastReadParam {
                wait_states: p.wait_states_222()?,
                mode_bits: p.mode_bits_222()?,
                opcode: p.read_opcode_222()?,
            }
        }
        if jedec.support_fast_read_444 {
            jedec.param_444 = FastReadParam {
                wait_states: p.wait_states_444()?,
                mode_bits: p.mode_bits_444()?,
                opcode: p.read_opcode_444()?,
            }
        }
        if p.data.len() >= 16 {
            let mult = 2 * (p.erase_time_multiplier()? + 1);
            let type1 = Self::ERASE_TIME_UNITS[p.erase_type1_unit()?] * (p.erase_type1_time()? + 1);
            let type2 = Self::ERASE_TIME_UNITS[p.erase_type2_unit()?] * (p.erase_type2_time()? + 1);
            let type3 = Self::ERASE_TIME_UNITS[p.erase_type3_unit()?] * (p.erase_type3_time()? + 1);
            let type4 = Self::ERASE_TIME_UNITS[p.erase_type4_unit()?] * (p.erase_type4_time()? + 1);
            jedec.erase[0].time = Some(TimeBound {
                typical: type1,
                maximum: type1 * mult,
            });
            jedec.erase[1].time = Some(TimeBound {
                typical: type2,
                maximum: type2 * mult,
            });
            jedec.erase[2].time = Some(TimeBound {
                typical: type3,
                maximum: type3 * mult,
            });
            jedec.erase[3].time = Some(TimeBound {
                typical: type4,
                maximum: type4 * mult,
            });

            let mult = 2 * (p.pgm_time_multiplier()? + 1);
            let chip_erase_unit = Self::CHIP_ERASE_UNITS[p.chip_erase_unit()?];
            let page_pgm_unit = Self::PAGE_PGM_UNITS[p.page_pgm_unit()?];
            let byte_pgm_unit = Self::BYTE_PGM_UNITS[p.byte_pgm_unit()?];
            let additional_byte_pgm_unit = Self::BYTE_PGM_UNITS[p.additional_byte_pgm_unit()?];

            jedec.rev_b = Some(JedecParamsRevB {
                page_size: 1u32 << p.page_size()?,
                page_program_time: TimeBound {
                    typical: page_pgm_unit * (1 + p.page_pgm_time()?),
                    maximum: page_pgm_unit * (1 + p.page_pgm_time()?) * mult,
                },
                byte_program_time: TimeBound {
                    typical: byte_pgm_unit * (1 + p.byte_pgm_time()?),
                    maximum: byte_pgm_unit * (1 + p.byte_pgm_time()?) * mult,
                },
                additional_byte_program_time: TimeBound {
                    typical: additional_byte_pgm_unit * (1 + p.additional_byte_pgm_time()?),
                    maximum: additional_byte_pgm_unit * (1 + p.additional_byte_pgm_time()?) * mult,
                },
                chip_erase_time: TimeBound {
                    typical: chip_erase_unit * (1 + p.chip_erase_time()?),
                    maximum: chip_erase_unit * (1 + p.chip_erase_time()?) * mult,
                },

                suspend_resume_supported: !p.suspend_resume_supported()?,
                prohibited_ops_program_suspend: p.prohibited_pgm_suspend()?,
                prohibited_ops_erase_suspend: p.prohibited_erase_suspend()?,
                program_resume_to_suspend: Duration::from_millis(64)
                    * (1 + p.pgm_resume_to_suspend()?),
                suspend_in_progress_program_latency: Self::SUSPEND_RESUME_UNITS
                    [p.suspend_pgm_latency_unit()?]
                    * (1 + p.suspend_pgm_latency_time()?),
                erase_resume_to_suspend: Duration::from_millis(64)
                    * (1 + p.erase_resume_to_suspend()?),
                suspend_in_progress_erase_latency: Self::SUSPEND_RESUME_UNITS
                    [p.suspend_erase_latency_unit()?]
                    * (1 + p.suspend_erase_latency_time()?),
                program_resume_instruction: p.pgm_resume_instruction()?,
                program_suspend_instruction: p.pgm_suspend_instruction()?,
                resume_instruction: p.suspend_instruction()?,
                suspend_instruction: p.resume_instruction()?,

                deep_powerdown_supported: !p.deep_powerdown_supported()?,
                enter_deep_powerdown_instruction: p.enter_deep_powerdown_instruction()?,
                exit_deep_powerdown_instruction: p.exit_deep_powerdown_instruction()?,
                exit_deep_powerdown_delay: Self::SUSPEND_RESUME_UNITS
                    [p.exit_deep_powerdown_unit()?]
                    * (1 + p.exit_deep_powerdown_time()?),
                status_register_polling: p.status_busy_polling()?,

                hold_or_reset_disable: p.hold_or_reset_disable()?,
                quad_enable_requirements: p.quad_enable_requirements()?,
                mode_444_entry: p.mode_444_entry()?,
                mode_444_exit: p.mode_444_exit()?,
                mode_444_supported: p.mode_444_supported()?,
                mode_444_disable: p.mode_444_disable()?,
                mode_444_enable: p.mode_444_enable()?,

                enter_4b_addressing: p.enter_4b_addressing()?,
                exit_4b_addressing: p.exit_4b_addressing()?,
                soft_reset_support: p.soft_reset_support()?,
                status_reg1_write_enable: p.status_reg1_write_enable()?,
            });
        }
        if p.data.len() >= 20 {
            jedec.rev_d = Some(JedecParamsRevD {
                param_118: FastReadParam {
                    wait_states: p.wait_states_118()?,
                    mode_bits: p.mode_bits_118()?,
                    opcode: p.read_opcode_118()?,
                },
                param_188: FastReadParam {
                    wait_states: p.wait_states_188()?,
                    mode_bits: p.mode_bits_188()?,
                    opcode: p.read_opcode_188()?,
                },

                output_driver_strength: p.output_driver_strength()?,
                jedec_spi_protocol_reset: p.spi_protocol_reset()?,
                data_strobe_waveform_str: p.data_strobe_str()?,
                data_strobe_support_4s4s4s: p.data_qpi_str()?,
                data_strobe_support_4d4d4d: p.data_qpi_dtr()?,
                octal_dtr_command: p.octal_dtr_command()?,
                octal_byte_order: p.octal_byte_order()?,

                mode_8s8s8s_disable: p.mode_8s8s8s_disable()?,
                mode_8s8s8s_enable: p.mode_8s8s8s_enable()?,
                mode_088_supported: p.mode_088_supported()?,
                mode_088_exit: p.mode_088_exit()?,
                mode_088_enter: p.mode_088_enter()?,
                octal_enable_requirements: p.octal_enable_requirements()?,

                max_speed_4s4s4s_no_strobe: p.max_speed_4s4s4s_no_strobe()?,
                max_speed_4s4s4s_strobe: p.max_speed_4s4s4s_strobe()?,
                max_speed_4d4d4d_no_strobe: p.max_speed_4d4d4d_no_strobe()?,
                max_speed_4d4d4d_strobe: p.max_speed_4d4d4d_strobe()?,
                max_speed_8s8s8s_no_strobe: p.max_speed_8s8s8s_no_strobe()?,
                max_speed_8s8s8s_strobe: p.max_speed_8s8s8s_strobe()?,
                max_speed_8d8d8d_no_strobe: p.max_speed_8d8d8d_no_strobe()?,
                max_speed_8d8d8d_strobe: p.max_speed_8d8d8d_strobe()?,
            });
        }
        if p.data.len() >= 23 {
            let mut rev_f = JedecParamsRevF::default();
            if p.support_fast_read_1s1d1d()? {
                rev_f.param_1s1d1d = Some(FastReadParam {
                    wait_states: p.wait_states_1s1d1d()?,
                    mode_bits: p.mode_bits_1s1d1d()?,
                    opcode: p.read_opcode_1s1d1d()?,
                });
            }
            if p.support_fast_read_1s2d2d()? {
                rev_f.param_1s2d2d = Some(FastReadParam {
                    wait_states: p.wait_states_1s2d2d()?,
                    mode_bits: p.mode_bits_1s2d2d()?,
                    opcode: p.read_opcode_1s2d2d()?,
                });
            }
            if p.support_fast_read_1s4d4d()? {
                rev_f.param_1s4d4d = Some(FastReadParam {
                    wait_states: p.wait_states_1s4d4d()?,
                    mode_bits: p.mode_bits_1s4d4d()?,
                    opcode: p.read_opcode_1s4d4d()?,
                });
            }
            if p.support_fast_read_4s4d4d()? {
                rev_f.param_4s4d4d = Some(FastReadParam {
                    wait_states: p.wait_states_4s4d4d()?,
                    mode_bits: p.mode_bits_4s4d4d()?,
                    opcode: p.read_opcode_4s4d4d()?,
                });
            }
            jedec.rev_f = Some(rev_f);
        }
        Ok(jedec)
    }
}

/// An `UnknownParams` structure represents SFDP parameter tables for which
/// we don't have a specialized parser.
#[derive(Debug, Serialize)]
pub struct UnknownParams {
    pub data: Vec<u32>,
}

impl TryFrom<&[u8]> for UnknownParams {
    type Error = Error;
    fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
        let mut reader = std::io::Cursor::new(buf);
        let mut data = vec![0u32; buf.len() / 4];
        reader.read_u32_into::<LittleEndian>(&mut data)?;
        Ok(UnknownParams { data })
    }
}

/// The `Sfdp` structure represents the decoded SFDP table.
#[derive(Debug, Serialize)]
pub struct Sfdp {
    pub header: SfdpHeader,
    pub phdr: Vec<SfdpPhdr>,
    pub jedec: JedecParams,
    pub params: Vec<UnknownParams>,
}

impl Sfdp {
    /// Given an initial SFDP buffer calculate the number of bytes needed for
    /// the entire SFDP.
    pub fn length_required(buf: &[u8]) -> Result<usize, Error> {
        if buf.len() < 256 {
            // Technically, we could read out the first 8-bytes, then
            // figure out how many phdrs there are, read out that number
            // times 8-byte chunks, then find the max extent of the pointed-to
            // headers and perform the calculation in the else-arm below.
            //
            // We punt and read out the first 256 bytes, which is often more
            // than enough.  In the event it isn't, we assume the first 256
            // bytes will contain all of the phdrs (it will) and perform
            // the max-extent calculation below.
            Ok(256)
        } else {
            let header = SfdpHeader::try_from(&buf[0..8])?;
            let mut len = 0;
            for i in 0..=header.nph {
                let start = (8 + i * 8) as usize;
                let end = start + 8;
                let phdr = SfdpPhdr::try_from(&buf[start..end])?;
                len = std::cmp::max(len, phdr.offset + (phdr.dwords * 4) as u32);
                log::debug!("computed sfdp len = {}", len);
            }
            Ok(len as usize)
        }
    }
}

/// Convert a byte buffer into an SFDP structure.
impl TryFrom<&[u8]> for Sfdp {
    type Error = anyhow::Error;
    fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
        let header = SfdpHeader::try_from(buf.get(0..8).ok_or(Error::SliceRange(0, 8))?)?;
        let mut phdr = Vec::new();
        for i in 0..=header.nph {
            let start = (8 + i * 8) as usize;
            let end = start + 8;
            phdr.push(SfdpPhdr::try_from(
                buf.get(start..end).ok_or(Error::SliceRange(start, end))?,
            )?);
        }

        let start = phdr[0].offset as usize;
        let end = start + phdr[0].dwords as usize * 4;
        let jedec =
            JedecParams::try_from(buf.get(start..end).ok_or(Error::SliceRange(start, end))?)?;

        let mut params = Vec::new();
        for ph in phdr.iter().take((header.nph as usize) + 1).skip(1) {
            let start = ph.offset as usize;
            let end = start + ph.dwords as usize * 4;
            params.push(UnknownParams::try_from(
                buf.get(start..end).ok_or(Error::SliceRange(start, end))?,
            )?);
        }

        Ok(Sfdp {
            header,
            phdr,
            jedec,
            params,
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use anyhow::Result;
    use humantime::parse_duration;

    #[rustfmt::skip]
    const SFDP_MX66L1G: &[u8; 512] = include_bytes!("SFDP_MX66L1G.bin");

    #[allow(clippy::bool_assert_comparison)]
    #[test]
    fn test_decode_mx66l1g() -> Result<()> {
        let sfdp = Sfdp::try_from(&SFDP_MX66L1G[..])?;
        // A simple spot-check of values from the SFDP table.
        assert_eq!(sfdp.header.signature, 0x50444653);
        assert_eq!(sfdp.header.major, 1);
        assert_eq!(sfdp.header.minor, 6);
        assert_eq!(sfdp.header.nph, 2);

        assert_eq!(sfdp.jedec.block_erase_size, BlockEraseSize::Block4KiB);
        assert_eq!(sfdp.jedec.write_en_required, false);
        assert_eq!(sfdp.jedec.write_en_opcode, 0x06);
        assert_eq!(sfdp.jedec.erase_opcode_4kib, 0x20);
        assert_eq!(sfdp.jedec.support_fast_read_112, true);
        assert_eq!(sfdp.jedec.address_modes, SupportedAddressModes::Mode3b4b);
        assert_eq!(sfdp.jedec.density, 128 * 1024 * 1024);

        assert_eq!(sfdp.jedec.erase[0].size, 4096);
        assert_eq!(sfdp.jedec.erase[0].opcode, 0x20);
        // The datasheet says typ=30ms, max=400ms.  The SFDP-reported
        // erase timings are advisory, have a limited representations
        // and won't match the data sheet exactly.
        assert_eq!(
            sfdp.jedec.erase[0].time.as_ref().unwrap().typical,
            parse_duration("30ms")?
        );
        assert_eq!(
            sfdp.jedec.erase[0].time.as_ref().unwrap().maximum,
            parse_duration("420ms")?
        );

        assert_eq!(sfdp.jedec.erase[1].size, 32768);
        assert_eq!(sfdp.jedec.erase[1].opcode, 0x52);
        // The datasheet says typ=0.15s, max=1s.
        assert_eq!(
            sfdp.jedec.erase[1].time.as_ref().unwrap().typical,
            parse_duration("160ms")?
        );
        assert_eq!(
            sfdp.jedec.erase[1].time.as_ref().unwrap().maximum,
            parse_duration("2s 240ms")?
        );

        assert_eq!(sfdp.jedec.erase[2].size, 65536);
        assert_eq!(sfdp.jedec.erase[2].opcode, 0xd8);
        // The datasheet says typ=0.28s, max=2s.
        assert_eq!(
            sfdp.jedec.erase[2].time.as_ref().unwrap().typical,
            parse_duration("288ms")?
        );
        assert_eq!(
            sfdp.jedec.erase[2].time.as_ref().unwrap().maximum,
            parse_duration("4s 32ms")?
        );

        assert_eq!(sfdp.jedec.erase[3].size, 0);
        assert_eq!(sfdp.jedec.erase[3].opcode, 0xff);

        let rev_b = sfdp.jedec.rev_b.as_ref().expect("rev_b parameters");
        assert_eq!(rev_b.page_size, 256);
        // The datasheet says typ=0.25ms, max=3ms.
        assert_eq!(rev_b.page_program_time.typical, parse_duration("256us")?);
        assert_eq!(rev_b.page_program_time.maximum, parse_duration("3ms 72us")?);
        // The datasheet says typ=25us, max=60us.
        assert_eq!(rev_b.byte_program_time.typical, parse_duration("32us")?);
        assert_eq!(rev_b.byte_program_time.maximum, parse_duration("384us")?);
        // The datasheet doesn't mention the additional byte programming time.
        assert_eq!(
            rev_b.additional_byte_program_time.typical,
            parse_duration("1us")?
        );
        assert_eq!(
            rev_b.additional_byte_program_time.maximum,
            parse_duration("12us")?
        );
        // The datasheet says typ=200s, max=600s. The typical time is the closest
        // possible representation given the range and units available in the SFDP.
        assert_eq!(rev_b.chip_erase_time.typical, parse_duration("4m 16s")?);
        assert_eq!(rev_b.chip_erase_time.maximum, parse_duration("51m 12s")?);

        // The particular MX66L1G sampled doesn't have a RevD or RevF table.
        assert!(sfdp.jedec.rev_d.is_none());
        assert!(sfdp.jedec.rev_f.is_none());
        Ok(())
    }

    // Regression test for https://github.com/lowRISC/opentitan/issues/13477
    #[test]
    fn test_bad_header_signature() -> Result<()> {
        let buf = &[255u8; 256];
        let sfdp = Sfdp::try_from(&buf[..]);
        assert!(sfdp.is_err());
        let err = sfdp.unwrap_err();
        assert_eq!(
            err.to_string(),
            "SFDP header contains incorrect signature: 0xffffffff"
        );
        Ok(())
    }
}