Edward O'Callaghan has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/74166 )
Change subject: util/flashchips_db_parser: Add initial rust flashchip json parser ......................................................................
util/flashchips_db_parser: Add initial rust flashchip json parser
Change-Id: I48346a3da5923e614728a1cc0567ed2bb1ba2e2f Signed-off-by: Edward O'Callaghan quasisec@google.com --- A util/flashchips_db_parser/Cargo.lock A util/flashchips_db_parser/Cargo.toml A util/flashchips_db_parser/src/main.rs A util/flashchips_db_parser/src/parser.rs 4 files changed, 403 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/66/74166/1
diff --git a/util/flashchips_db_parser/Cargo.lock b/util/flashchips_db_parser/Cargo.lock new file mode 100644 index 0000000..b8e7070 --- /dev/null +++ b/util/flashchips_db_parser/Cargo.lock @@ -0,0 +1,89 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "flashchips_db_parser_rust" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + +[[package]] +name = "proc-macro2" +version = "1.0.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + +[[package]] +name = "serde" +version = "1.0.159" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.159" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" diff --git a/util/flashchips_db_parser/Cargo.toml b/util/flashchips_db_parser/Cargo.toml new file mode 100644 index 0000000..a402ebc --- /dev/null +++ b/util/flashchips_db_parser/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "flashchips_db_parser_rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1.0.159", features = ["derive"] } +serde_json = "1.0.95" diff --git a/util/flashchips_db_parser/src/main.rs b/util/flashchips_db_parser/src/main.rs new file mode 100644 index 0000000..9ac1d4b --- /dev/null +++ b/util/flashchips_db_parser/src/main.rs @@ -0,0 +1,30 @@ +// +// This file is part of the flashrom project. +// +// Copyright (C) 2023 Google Australia. +// +// 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. +// + +use std::fs; + +pub mod parser; + +fn main() { + let file_path = "../../data/flashchips.json".to_owned(); + let contents = fs::read_to_string(file_path).expect("Couldn't find or load that file."); + + let result = parser::flashchips(&contents); + match result { + Ok(result) => (), + Err(error) => print!("{}", error), + } +} diff --git a/util/flashchips_db_parser/src/parser.rs b/util/flashchips_db_parser/src/parser.rs new file mode 100644 index 0000000..c02799c --- /dev/null +++ b/util/flashchips_db_parser/src/parser.rs @@ -0,0 +1,264 @@ +// +// This file is part of the flashrom project. +// +// Copyright (C) 2023 Google Australia. +// +// 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. +// + +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; +use serde_json::Result; //{Result, Value}; + +//#[derive(Serialize, Deserialize)] +//enum ChipBusType { +// BUS_NONE = 0, +// BUS_PARALLEL = 1 << 0, +// BUS_LPC = 1 << 1, +// BUS_FWH = 1 << 2, +// BUS_SPI = 1 << 3, +// BUS_PROG = 1 << 4, +// BUS_NONSPI = (1 << 0) | (1 << 1) | (1 << 2), // BUS_PARALLEL | BUS_LPC | BUS_FWH, +//} + +#[derive(Serialize, Deserialize)] +enum WriteGranularity { + WRITE_GRAN_256BYTES = 0, + WRITE_GRAN_1BIT, + WRITE_GRAN_1BYTE, + WRITE_GRAN_128BYTES, + WRITE_GRAN_264BYTES, + WRITE_GRAN_512BYTES, + WRITE_GRAN_528BYTES, + WRITE_GRAN_1024BYTES, + WRITE_GRAN_1056BYTES, + WRITE_GRAN_1BYTE_IMPLICIT_ERASE, +} + +#[derive(Serialize, Deserialize)] +struct EraseBlock { + size: u32, + count: u32, +} + +#[derive(Serialize, Deserialize)] +enum BlockEraseFunc { + NO_BLOCK_ERASE_FUNC = 0, /* 0 indicates no block erase function set. */ + SPI_BLOCK_ERASE_EMULATION = 1, + SPI_BLOCK_ERASE_20, + SPI_BLOCK_ERASE_21, + SPI_BLOCK_ERASE_40, + SPI_BLOCK_ERASE_50, + SPI_BLOCK_ERASE_52, + SPI_BLOCK_ERASE_53, + SPI_BLOCK_ERASE_5C, + SPI_BLOCK_ERASE_60, + SPI_BLOCK_ERASE_62, + SPI_BLOCK_ERASE_81, + SPI_BLOCK_ERASE_C4, + SPI_BLOCK_ERASE_C7, + SPI_BLOCK_ERASE_D7, + SPI_BLOCK_ERASE_D8, + SPI_BLOCK_ERASE_DB, + SPI_BLOCK_ERASE_DC, + S25FL_BLOCK_ERASE, + S25FS_BLOCK_ERASE_D8, + JEDEC_SECTOR_ERASE, + JEDEC_BLOCK_ERASE, + JEDEC_CHIP_BLOCK_ERASE, + OPAQUE_ERASE, + SPI_ERASE_AT45CS_SECTOR, + SPI_ERASE_AT45DB_BLOCK, + SPI_ERASE_AT45DB_CHIP, + SPI_ERASE_AT45DB_PAGE, + SPI_ERASE_AT45DB_SECTOR, + ERASE_CHIP_28SF040, + ERASE_SECTOR_28SF040, + ERASE_BLOCK_82802AB, + ERASE_SECTOR_49LFXXXC, + STM50_SECTOR_ERASE, + EDI_CHIP_BLOCK_ERASE, +} + +#[derive(Serialize, Deserialize)] +struct BlockEraser { + eraseblocks: Option<Vec<EraseBlock>>, + block_erase: BlockEraseFunc, +} + +#[derive(Serialize, Deserialize)] +struct Voltage { + min: u16, + max: u16, +} + +#[derive(Serialize, Deserialize, Debug)] +enum FlashReg { + INVALID_REG = 0, + STATUS1, + STATUS2, + STATUS3, + SECURITY, + CONFIG, + MAX_REGISTERS, +} + +#[derive(Serialize, Deserialize, Debug)] +enum Writability { + RO, + RW, + OTP, +} + +#[derive(Serialize, Deserialize, Debug)] +struct RegBitInfo { + reg: FlashReg, + bit_index: u8, + writability: Writability, +} + +#[derive(Serialize, Deserialize, Debug)] +struct RegBitMap { + srp: Option<RegBitInfo>, + srl: Option<RegBitInfo>, + bp: Vec<RegBitInfo>, + tb: Option<RegBitInfo>, + sec: Option<RegBitInfo>, + cmp: Option<RegBitInfo>, + wps: Option<RegBitInfo>, +} + +#[derive(Serialize, Deserialize)] +enum DecodeRangeFunc { + NO_DECODE_RANGE_FUNC = 0, /* 0 indicates no range decode function is set. */ + DECODE_RANGE_SPI25 = 1, + DECODE_RANGE_SPI25_64K_BLOCK = 2, + DECODE_RANGE_SPI25_BIT_CMP = 3, + DECODE_RANGE_SPI25_2X_BLOCK = 4, +} + +#[derive(Serialize, Deserialize)] +struct Tested { + probe: u32, // enum test_state probe; + read: u32, // enum test_state read; + erase: u32, // enum test_state erase; + write: u32, // enum test_state write; + wp: u32, // enum test_state wp; +} + +#[derive(Serialize, Deserialize)] +enum ProbeFunc { + NO_PROBE_FUNC = 0, /* 0 indicates no probe function set. */ + PROBE_JEDEC = 1, + PROBE_JEDEC_29GL, + PROBE_OPAQUE, + PROBE_EDI_KB9012, + PROBE_AT82802AB, + PROBE_W29EE011, + PROBE_EN29LV640B, + PROBE_SPI_AT25F, + PROBE_SPI_AT45DB, + PROBE_SPI_BIG_SPANSION, + PROBE_SPI_RDID, + PROBE_SPI_RDID4, + PROBE_SPI_REMS, + PROBE_SPI_RES1, + PROBE_SPI_RES2, + PROBE_SPI_SFDP, + PROBE_SPI_ST95, +} + +#[derive(Serialize, Deserialize)] +enum WriteFunc { + NO_WRITE_FUNC = 0, /* 0 indicates no write function set. */ + WRITE_JEDEC = 1, + WRITE_JEDEC1, + WRITE_OPAQUE, + SPI_CHIP_WRITE1, + SPI_CHIP_WRITE256, + SPI_WRITE_AAI, + SPI_WRITE_AT45DB, + WRITE_28SF040, + WRITE_82802AB, + WRITE_EN29LV640B, + EDI_CHIP_WRITE, +} + +#[derive(Serialize, Deserialize, Debug)] // XXX +enum ReadFunc { + NO_READ_FUNC = 0, /* 0 indicates no read function set. */ + SPI_CHIP_READ = 1, + READ_OPAQUE, + READ_MEMMAPPED, + EDI_CHIP_READ, + SPI_READ_AT45DB, + SPI_READ_AT45DB_E8, +} + +#[derive(Serialize, Deserialize)] +enum SpiCmdSet { + SPI25 = 0, + SPI_EDI = 1, +} + +#[derive(Serialize, Deserialize)] +struct FlashChip { + vendor: String, + name: String, + + bustype: String, // XXX Vec<ChipBusType>, + + manufacture_id: u32, + model_id: u32, + + total_size: u32, + page_size: u32, + + feature_bits: u32, + + tested: Option<Tested>, + + spi_cmd_set: SpiCmdSet, + + probe: ProbeFunc, + + probe_timing: i32, + + block_erasers: Vec<BlockEraser>, + + printlock: Option<u32>, // Obsolete printlock_func + unlock: Option<u32>, // Obsolete blockprotect_func + + write: WriteFunc, + read: ReadFunc, + + voltage: Voltage, + + gran: WriteGranularity, + + reg_bits: Option<RegBitMap>, + + decode_range: Option<DecodeRangeFunc>, +} + +pub fn flashchips(json_data: &str) -> Result<()> { + //let v: Value = serde_json::from_str(json_data)?; + let m: HashMap<String, Vec<FlashChip>> = serde_json::from_str(json_data)?; + let v: &Vec<FlashChip> = &m["flashchips"]; + + for fc in v { + println!("{}", fc.name); +// println!("read func {:#?}", fc.read); + } + + Ok(()) +}