pub fn new(
        size: u64,
        fd_blk: Option<File>,
        block_len: u32,
        bank_width: u32,
        device_width: u32,
        read_only: bool,
    ) -> Result<Self> {
        if block_len == 0 {
            bail!("PFlash: block-length is zero which is invalid.");
        }
        let blocks_per_device: u32 = size as u32 / block_len;
        if blocks_per_device == 0 {
            bail!("PFlash: num-blocks is zero which is invalid.");
        }

        if let Some(fd) = &fd_blk {
            let file_size = fd.metadata().unwrap().len();
            if file_size < size {
                bail!(
                    "PFlash requires 0x{:X} bytes, given file provides 0x{:X} bytes",
                    size,
                    file_size
                );
            }
        }

        let num_devices: u32 = if device_width == 0 {
            1
        } else {
            bank_width / device_width
        };
        let block_len_per_device: u32 = block_len / num_devices;
        let device_len: u32 = block_len_per_device * blocks_per_device;

        let mut cfi_table: [u8; 0x52] = [0; 0x52];
        // Standard "QRY" string for CFI query.
        cfi_table[0x10] = b'Q';
        cfi_table[0x11] = b'R';
        cfi_table[0x12] = b'Y';
        // Primary vendor command set and control interface ID code (Intel).
        cfi_table[0x13] = 0x01;
        cfi_table[0x14] = 0x00;
        // Table address of primary algorithm for extended query.
        cfi_table[0x15] = 0x31;
        cfi_table[0x16] = 0x00;
        // Vcc logic supplies minimum program/erase voltage.
        cfi_table[0x1B] = 0x45;
        // Vcc logic supplies maximum program/erase voltage.
        cfi_table[0x1C] = 0x55;
        // Typical timeout per single byte/word write (buffer write count = 1), 2^n μs.
        cfi_table[0x1F] = 0x07;
        // Typical timeout for maximum-size buffer write, 2^n μs.
        cfi_table[0x20] = 0x07;
        // Typical timeout per individual block erase, 2^n ms.
        cfi_table[0x21] = 0x0a;
        // Typical timeout for full chip erase (not supported).
        cfi_table[0x22] = 0x00;
        // Maximum timeout for byte/word write, 2^n times typically.
        cfi_table[0x23] = 0x04;
        // Maximum timeout for buffer write, 2^n times typically.
        cfi_table[0x24] = 0x04;
        // Maximum timeout per individual block erase, 2^n times typically.
        cfi_table[0x25] = 0x04;
        // Maximum timeout for chip erase, 2n times typically (not supported).
        cfi_table[0x26] = 0x00;
        // Device size, 2^n in number of bytes.
        cfi_table[0x27] = device_len.trailing_zeros() as u8;
        // PFlash device interface description (8 & 16 bits).
        cfi_table[0x28] = 0x02;
        cfi_table[0x29] = 0x00;
        // Max number of bytes (2^n) in multi-bytes write.
        cfi_table[0x2A] = if bank_width == 1 { 0x08 } else { 0x0B };
        cfi_table[0x2B] = 0x00;

        let mut write_blk_size: u32 = 1 << cfi_table[0x2A];
        if num_devices > 1 {
            write_blk_size *= num_devices;
        }

        // Number of erase block regions within device (uniform).
        cfi_table[0x2C] = 0x01;
        // Information about block region erase.
        cfi_table[0x2D] = (blocks_per_device - 1) as u8;
        cfi_table[0x2E] = ((blocks_per_device - 1) >> 8) as u8;
        cfi_table[0x2F] = (block_len_per_device >> 8) as u8;
        cfi_table[0x30] = (block_len_per_device >> 16) as u8;

        // Primary extended query table, SCS not supported.
        cfi_table[0x31] = b'P';
        cfi_table[0x32] = b'R';
        cfi_table[0x33] = b'I';
        // Major version number.
        cfi_table[0x34] = b'1';
        // Minor version number.
        cfi_table[0x35] = b'0';
        // Number of protection fields.
        cfi_table[0x3f] = 0x01;

        Ok(PFlash {
            fd_blk,
            block_len,
            bank_width,
            // device id for Intel PFlash.
            ident: [0x89, 0x18, 0x00, 0x00],
            blk_num: blocks_per_device,
            device_width,
            max_device_width: device_width,
            write_cycle: 0,
            read_only,
            cmd: 0,
            status: 0x80,
            cfi_table,
            counter: 0,
            write_blk_size,
            rom: None,
            res: SysRes::default(),
        })
    }
/// Construct function of PFlash device.
///
/// # Arguments
///
/// * `size` - PFlash device size.
/// * `fd_blk` - The file descriptor of PFlash device which emulates disk storage.
/// * `block_len` - The length of block in PFlash device.
/// * `bank_width` - The width of PFlash array which contains this PFlash device.
/// * `device_width` - The width of this PFlash device.
/// * `read_only` - This PFlash device is read only or not.
///
/// # Errors
///
/// Return Error when
/// * block-length is zero.
/// * PFlash size is zero.
/// * file size is smaller than PFlash size.
Construct function of PFlash device.
Construct function of PFlash device .
pub fn new(
        size: u64,
        fd_blk: Option<File>,
        block_len: u32,
        bank_width: u32,
        device_width: u32,
        read_only: bool,
    ) -> Result<Self> {
        if block_len == 0 {
            bail!("PFlash: block-length is zero which is invalid.");
        }
        let blocks_per_device: u32 = size as u32 / block_len;
        if blocks_per_device == 0 {
            bail!("PFlash: num-blocks is zero which is invalid.");
        }
        if let Some(fd) = &fd_blk {
            let file_size = fd.metadata().unwrap().len();
            if file_size < size {
                bail!(
                    "PFlash requires 0x{:X} bytes, given file provides 0x{:X} bytes",
                    size,
                    file_size
                );
            }
        }
        let num_devices: u32 = if device_width == 0 {
            1
        } else {
            bank_width / device_width
        };
        let block_len_per_device: u32 = block_len / num_devices;
        let device_len: u32 = block_len_per_device * blocks_per_device;
        let mut cfi_table: [u8; 0x52] = [0; 0x52];
        // Standard "QRY" string for CFI query.
        cfi_table[0x10] = b'Q';
        cfi_table[0x11] = b'R';
        cfi_table[0x12] = b'Y';
        // Primary vendor command set and control interface ID code (Intel).
        cfi_table[0x13] = 0x01;
        cfi_table[0x14] = 0x00;
        // Table address of primary algorithm for extended query.
        cfi_table[0x15] = 0x31;
        cfi_table[0x16] = 0x00;
        // Vcc logic supplies minimum program/erase voltage.
        cfi_table[0x1B] = 0x45;
        // Vcc logic supplies maximum program/erase voltage.
        cfi_table[0x1C] = 0x55;
        // Typical timeout per single byte/word write (buffer write count = 1), 2^n μs.
        cfi_table[0x1F] = 0x07;
        // Typical timeout for maximum-size buffer write, 2^n μs.
        cfi_table[0x20] = 0x07;
        // Typical timeout per individual block erase, 2^n ms.
        cfi_table[0x21] = 0x0a;
        // Typical timeout for full chip erase (not supported).
        cfi_table[0x22] = 0x00;
        // Maximum timeout for byte/word write, 2^n times typically.
        cfi_table[0x23] = 0x04;
        // Maximum timeout for buffer write, 2^n times typically.
        cfi_table[0x24] = 0x04;
        // Maximum timeout per individual block erase, 2^n times typically.
        cfi_table[0x25] = 0x04;
        // Maximum timeout for chip erase, 2n times typically (not supported).
        cfi_table[0x26] = 0x00;
        // Device size, 2^n in number of bytes.
        cfi_table[0x27] = device_len.trailing_zeros() as u8;
        // PFlash device interface description (8 & 16 bits).
        cfi_table[0x28] = 0x02;
        cfi_table[0x29] = 0x00;
        // Max number of bytes (2^n) in multi-bytes write.
        cfi_table[0x2A] = if bank_width == 1 { 0x08 } else { 0x0B };
        cfi_table[0x2B] = 0x00;
        let mut write_blk_size: u32 = 1 << cfi_table[0x2A];
        if num_devices > 1 {
            write_blk_size *= num_devices;
        }
        // Number of erase block regions within device (uniform).
        cfi_table[0x2C] = 0x01;
        // Information about block region erase.
        cfi_table[0x2D] = (blocks_per_device - 1) as u8;
        cfi_table[0x2E] = ((blocks_per_device - 1) >> 8) as u8;
        cfi_table[0x2F] = (block_len_per_device >> 8) as u8;
        cfi_table[0x30] = (block_len_per_device >> 16) as u8;
        // Primary extended query table, SCS not supported.
        cfi_table[0x31] = b'P';
        cfi_table[0x32] = b'R';
        cfi_table[0x33] = b'I';
        // Major version number.
        cfi_table[0x34] = b'1';
        // Minor version number.
        cfi_table[0x35] = b'0';
        // Number of protection fields.
        cfi_table[0x3f] = 0x01;
        Ok(PFlash {
            fd_blk,
            block_len,
            bank_width,
            // device id for Intel PFlash.
            ident: [0x89, 0x18, 0x00, 0x00],
            blk_num: blocks_per_device,
            device_width,
            max_device_width: device_width,
            write_cycle: 0,
            read_only,
            cmd: 0,
            status: 0x80,
            cfi_table,
            counter: 0,
            write_blk_size,
            rom: None,
            res: SysRes::default(),
        })
    }
pub fn new ( size : u64 , fd_blk : Option < File > , block_len : u32 , bank_width : u32 , device_width : u32 , read_only : bool , ) -> Result < Self > { if block_len == 0 { bail ! ( " " ) ; } let blocks_per_device : u32 = size as u32 / block_len ; if blocks_per_device == 0 { bail ! ( " " ) ; } if let Some ( fd ) = & fd_blk { let file_size = fd . metadata ( ) . unwrap ( ) . len ( ) ; if file_size < size { bail ! ( " " size file_size ) ; } } let num_devices : u32 = if device_width == 0 { 1 } else { bank_width / device_width } ; let block_len_per_device : u32 = block_len / num_devices ; let device_len : u32 = block_len_per_device * blocks_per_device ; let mut cfi_table : [ u8 ; 0x52 ] = [ 0 ; 0x52 ] ; // Standard "QRY" string for CFI query. cfi_table [ 0x10 ] = b'Q' ; cfi_table [ 0x11 ] = b'R' ; cfi_table [ 0x12 ] = b'Y' ; // Primary vendor command set and control interface ID code (Intel). cfi_table [ 0x13 ] = 0x01 ; cfi_table [ 0x14 ] = 0x00 ; // Table address of primary algorithm for extended query. cfi_table [ 0x15 ] = 0x31 ; cfi_table [ 0x16 ] = 0x00 ; // Vcc logic supplies minimum program/erase voltage. cfi_table [ 0x1B ] = 0x45 ; // Vcc logic supplies maximum program/erase voltage. cfi_table [ 0x1C ] = 0x55 ; // Typical timeout per single byte/word write (buffer write count = 1), 2^n μs. cfi_table [ 0x1F ] = 0x07 ; // Typical timeout for maximum-size buffer write, 2^n μs. cfi_table [ 0x20 ] = 0x07 ; // Typical timeout per individual block erase, 2^n ms. cfi_table [ 0x21 ] = 0x0a ; // Typical timeout for full chip erase (not supported). cfi_table [ 0x22 ] = 0x00 ; // Maximum timeout for byte/word write, 2^n times typically. cfi_table [ 0x23 ] = 0x04 ; // Maximum timeout for buffer write, 2^n times typically. cfi_table [ 0x24 ] = 0x04 ; // Maximum timeout per individual block erase, 2^n times typically. cfi_table [ 0x25 ] = 0x04 ; // Maximum timeout for chip erase, 2n times typically (not supported). cfi_table [ 0x26 ] = 0x00 ; // Device size, 2^n in number of bytes. cfi_table [ 0x27 ] = device_len . trailing_zeros ( ) as u8 ; // PFlash device interface description (8 & 16 bits). cfi_table [ 0x28 ] = 0x02 ; cfi_table [ 0x29 ] = 0x00 ; // Max number of bytes (2^n) in multi-bytes write. cfi_table [ 0x2A ] = if bank_width == 1 { 0x08 } else { 0x0B } ; cfi_table [ 0x2B ] = 0x00 ; let mut write_blk_size : u32 = 1 << cfi_table [ 0x2A ] ; if num_devices > 1 { write_blk_size *= num_devices ; } // Number of erase block regions within device (uniform). cfi_table [ 0x2C ] = 0x01 ; // Information about block region erase. cfi_table [ 0x2D ] = ( blocks_per_device - 1 ) as u8 ; cfi_table [ 0x2E ] = ( ( blocks_per_device - 1 ) >> 8 ) as u8 ; cfi_table [ 0x2F ] = ( block_len_per_device >> 8 ) as u8 ; cfi_table [ 0x30 ] = ( block_len_per_device >> 16 ) as u8 ; // Primary extended query table, SCS not supported. cfi_table [ 0x31 ] = b'P' ; cfi_table [ 0x32 ] = b'R' ; cfi_table [ 0x33 ] = b'I' ; // Major version number. cfi_table [ 0x34 ] = b'1' ; // Minor version number. cfi_table [ 0x35 ] = b'0' ; // Number of protection fields. cfi_table [ 0x3f ] = 0x01 ; Ok ( PFlash { fd_blk , block_len , bank_width , // device id for Intel PFlash. ident : [ 0x89 , 0x18 , 0x00 , 0x00 ] , blk_num : blocks_per_device , device_width , max_device_width : device_width , write_cycle : 0 , read_only , cmd : 0 , status : 0x80 , cfi_table , counter : 0 , write_blk_size , rom : None , res : SysRes :: default ( ) , } ) }
Construct function of PFlash device.
Construct function of PFlash device .
size
u64
fd_blk
Option<File>
block_len
u32
bank_width
u32
device_width
u32
read_only
bool
size
u64
fd_blk
Option<File>
block_len
u32
bank_width
u32
device_width
u32
read_only
bool
errors
Return Error when
block-length is zero.
PFlash size is zero.
file size is smaller than PFlash size.
Return Error when block - length is zero . PFlash size is zero . file size is smaller than PFlash size .