fn install_image(flashmap: &mut SimFlashMap, slots: &[SlotInfo], slot: usize, len: usize,
                 bad_sig: bool) -> [Option<Vec<u8>>; 2] {
    let offset = slots[slot].base_off;
    let slot_len = slots[slot].len;
    let dev_id = slots[slot].dev_id;

    let mut tlv = make_tlv();

    const HDR_SIZE: usize = 32;

    // Generate a boot header.  Note that the size doesn't include the header.
    let header = ImageHeader {
        magic: 0x96f3b83d,
        load_addr: 0,
        hdr_size: HDR_SIZE as u16,
        _pad1: 0,
        img_size: len as u32,
        flags: tlv.get_flags(),
        ver: ImageVersion {
            major: (offset / (128 * 1024)) as u8,
            minor: 0,
            revision: 1,
            build_num: offset as u32,
        },
        _pad2: 0,
    };

    let mut b_header = [0; HDR_SIZE];
    b_header[..32].clone_from_slice(header.as_raw());
    assert_eq!(b_header.len(), HDR_SIZE);

    tlv.add_bytes(&b_header);

    // The core of the image itself is just pseudorandom data.
    let mut b_img = vec![0; len];
    splat(&mut b_img, offset);

    // TLV signatures work over plain image
    tlv.add_bytes(&b_img);

    // Generate encrypted images
    let flag = TlvFlags::ENCRYPTED as u32;
    let is_encrypted = (tlv.get_flags() & flag) == flag;
    let mut b_encimg = vec![];
    if is_encrypted {
        let key = GenericArray::from_slice(AES_SEC_KEY);
        let nonce = GenericArray::from_slice(&[0; 16]);
        let mut cipher = Aes128Ctr::new(&key, &nonce);
        b_encimg = b_img.clone();
        cipher.apply_keystream(&mut b_encimg);
    }

    // Build the TLV itself.
    let mut b_tlv = if bad_sig {
        let good_sig = &mut tlv.make_tlv();
        vec![0; good_sig.len()]
    } else {
        tlv.make_tlv()
    };

    // Pad the block to a flash alignment (8 bytes).
    while b_tlv.len() % 8 != 0 {
        //FIXME: should be erase_val?
        b_tlv.push(0xFF);
    }

    let mut buf = vec![];
    buf.append(&mut b_header.to_vec());
    buf.append(&mut b_img);
    buf.append(&mut b_tlv.clone());

    let mut encbuf = vec![];
    if is_encrypted {
        encbuf.append(&mut b_header.to_vec());
        encbuf.append(&mut b_encimg);
        encbuf.append(&mut b_tlv);
    }

    let result: [Option<Vec<u8>>; 2];

    // Since images are always non-encrypted in slot0, we first write an
    // encrypted image, re-read to use for verification, erase + flash
    // un-encrypted. In slot1 the image is written un-encrypted, and if
    // encryption is requested, it follows an erase + flash encrypted.

    let flash = flashmap.get_mut(&dev_id).unwrap();

    if slot == 0 {
        let enc_copy: Option<Vec<u8>>;

        if is_encrypted {
            flash.write(offset, &encbuf).unwrap();

            let mut enc = vec![0u8; encbuf.len()];
            flash.read(offset, &mut enc).unwrap();

            enc_copy = Some(enc);

            flash.erase(offset, slot_len).unwrap();
        } else {
            enc_copy = None;
        }

        flash.write(offset, &buf).unwrap();

        let mut copy = vec![0u8; buf.len()];
        flash.read(offset, &mut copy).unwrap();

        result = [Some(copy), enc_copy];
    } else {

        flash.write(offset, &buf).unwrap();

        let mut copy = vec![0u8; buf.len()];
        flash.read(offset, &mut copy).unwrap();

        let enc_copy: Option<Vec<u8>>;

        if is_encrypted {
            flash.erase(offset, slot_len).unwrap();

            flash.write(offset, &encbuf).unwrap();

            let mut enc = vec![0u8; encbuf.len()];
            flash.read(offset, &mut enc).unwrap();

            enc_copy = Some(enc);
        } else {
            enc_copy = None;
        }

        result = [Some(copy), enc_copy];
    }

    result
}
/// Install a "program" into the given image.  This fakes the image header, or at least all of the
/// fields used by the given code.  Returns a copy of the image that was written.
Install a "program" into the given image.  This fakes the image header, or at least all of the
fields used by the given code.  Returns a copy of the image that was written.
Install a " program " into the given image . This fakes the image header or at least all of the fields used by the given code . Returns a copy of the image that was written .
fn install_image(flashmap: &mut SimFlashMap, slots: &[SlotInfo], slot: usize, len: usize,
                 bad_sig: bool) -> [Option<Vec<u8>>; 2] {
    let offset = slots[slot].base_off;
    let slot_len = slots[slot].len;
    let dev_id = slots[slot].dev_id;
    let mut tlv = make_tlv();
    const HDR_SIZE: usize = 32;
    // Generate a boot header.  Note that the size doesn't include the header.
    let header = ImageHeader {
        magic: 0x96f3b83d,
        load_addr: 0,
        hdr_size: HDR_SIZE as u16,
        _pad1: 0,
        img_size: len as u32,
        flags: tlv.get_flags(),
        ver: ImageVersion {
            major: (offset / (128 * 1024)) as u8,
            minor: 0,
            revision: 1,
            build_num: offset as u32,
        },
        _pad2: 0,
    };
    let mut b_header = [0; HDR_SIZE];
    b_header[..32].clone_from_slice(header.as_raw());
    assert_eq!(b_header.len(), HDR_SIZE);
    tlv.add_bytes(&b_header);
    // The core of the image itself is just pseudorandom data.
    let mut b_img = vec![0; len];
    splat(&mut b_img, offset);
    // TLV signatures work over plain image
    tlv.add_bytes(&b_img);
    // Generate encrypted images
    let flag = TlvFlags::ENCRYPTED as u32;
    let is_encrypted = (tlv.get_flags() & flag) == flag;
    let mut b_encimg = vec![];
    if is_encrypted {
        let key = GenericArray::from_slice(AES_SEC_KEY);
        let nonce = GenericArray::from_slice(&[0; 16]);
        let mut cipher = Aes128Ctr::new(&key, &nonce);
        b_encimg = b_img.clone();
        cipher.apply_keystream(&mut b_encimg);
    }
    // Build the TLV itself.
    let mut b_tlv = if bad_sig {
        let good_sig = &mut tlv.make_tlv();
        vec![0; good_sig.len()]
    } else {
        tlv.make_tlv()
    };
    // Pad the block to a flash alignment (8 bytes).
    while b_tlv.len() % 8 != 0 {
        //FIXME: should be erase_val?
        b_tlv.push(0xFF);
    }
    let mut buf = vec![];
    buf.append(&mut b_header.to_vec());
    buf.append(&mut b_img);
    buf.append(&mut b_tlv.clone());
    let mut encbuf = vec![];
    if is_encrypted {
        encbuf.append(&mut b_header.to_vec());
        encbuf.append(&mut b_encimg);
        encbuf.append(&mut b_tlv);
    }
    let result: [Option<Vec<u8>>; 2];
    // Since images are always non-encrypted in slot0, we first write an
    // encrypted image, re-read to use for verification, erase + flash
    // un-encrypted. In slot1 the image is written un-encrypted, and if
    // encryption is requested, it follows an erase + flash encrypted.
    let flash = flashmap.get_mut(&dev_id).unwrap();
    if slot == 0 {
        let enc_copy: Option<Vec<u8>>;
        if is_encrypted {
            flash.write(offset, &encbuf).unwrap();
            let mut enc = vec![0u8; encbuf.len()];
            flash.read(offset, &mut enc).unwrap();
            enc_copy = Some(enc);
            flash.erase(offset, slot_len).unwrap();
        } else {
            enc_copy = None;
        }
        flash.write(offset, &buf).unwrap();
        let mut copy = vec![0u8; buf.len()];
        flash.read(offset, &mut copy).unwrap();
        result = [Some(copy), enc_copy];
    } else {
        flash.write(offset, &buf).unwrap();
        let mut copy = vec![0u8; buf.len()];
        flash.read(offset, &mut copy).unwrap();
        let enc_copy: Option<Vec<u8>>;
        if is_encrypted {
            flash.erase(offset, slot_len).unwrap();
            flash.write(offset, &encbuf).unwrap();
            let mut enc = vec![0u8; encbuf.len()];
            flash.read(offset, &mut enc).unwrap();
            enc_copy = Some(enc);
        } else {
            enc_copy = None;
        }
        result = [Some(copy), enc_copy];
    }
    result
}
fn install_image ( flashmap : & mut SimFlashMap , slots : & [ SlotInfo ] , slot : usize , len : usize , bad_sig : bool ) -> [ Option < Vec < u8 > > ; 2 ] { let offset = slots [ slot ] . base_off ; let slot_len = slots [ slot ] . len ; let dev_id = slots [ slot ] . dev_id ; let mut tlv = make_tlv ( ) ; const HDR_SIZE : usize = 32 ; // Generate a boot header.  Note that the size doesn't include the header. let header = ImageHeader { magic : 0x96f3b83d , load_addr : 0 , hdr_size : HDR_SIZE as u16 , _pad1 : 0 , img_size : len as u32 , flags : tlv . get_flags ( ) , ver : ImageVersion { major : ( offset / ( 128 * 1024 ) ) as u8 , minor : 0 , revision : 1 , build_num : offset as u32 , } , _pad2 : 0 , } ; let mut b_header = [ 0 ; HDR_SIZE ] ; b_header [ .. 32 ] . clone_from_slice ( header . as_raw ( ) ) ; assert_eq ! ( b_header len ( ) HDR_SIZE ) ; tlv . add_bytes ( & b_header ) ; // The core of the image itself is just pseudorandom data. let mut b_img = vec ! [ 0 len ] ; splat ( & mut b_img , offset ) ; // TLV signatures work over plain image tlv . add_bytes ( & b_img ) ; // Generate encrypted images let flag = TlvFlags :: ENCRYPTED as u32 ; let is_encrypted = ( tlv . get_flags ( ) & flag ) == flag ; let mut b_encimg = vec ! [ ] ; if is_encrypted { let key = GenericArray :: from_slice ( AES_SEC_KEY ) ; let nonce = GenericArray :: from_slice ( & [ 0 ; 16 ] ) ; let mut cipher = Aes128Ctr :: new ( & key , & nonce ) ; b_encimg = b_img . clone ( ) ; cipher . apply_keystream ( & mut b_encimg ) ; } // Build the TLV itself. let mut b_tlv = if bad_sig { let good_sig = & mut tlv . make_tlv ( ) ; vec ! [ 0 good_sig len ( ) ] } else { tlv . make_tlv ( ) } ; // Pad the block to a flash alignment (8 bytes). while b_tlv . len ( ) % 8 != 0 { //FIXME: should be erase_val? b_tlv . push ( 0xFF ) ; } let mut buf = vec ! [ ] ; buf . append ( & mut b_header . to_vec ( ) ) ; buf . append ( & mut b_img ) ; buf . append ( & mut b_tlv . clone ( ) ) ; let mut encbuf = vec ! [ ] ; if is_encrypted { encbuf . append ( & mut b_header . to_vec ( ) ) ; encbuf . append ( & mut b_encimg ) ; encbuf . append ( & mut b_tlv ) ; } let result : [ Option < Vec < u8 > > ; 2 ] ; // Since images are always non-encrypted in slot0, we first write an // encrypted image, re-read to use for verification, erase + flash // un-encrypted. In slot1 the image is written un-encrypted, and if // encryption is requested, it follows an erase + flash encrypted. let flash = flashmap . get_mut ( & dev_id ) . unwrap ( ) ; if slot == 0 { let enc_copy : Option < Vec < u8 > > ; if is_encrypted { flash . write ( offset , & encbuf ) . unwrap ( ) ; let mut enc = vec ! [ 0u8 encbuf len ( ) ] ; flash . read ( offset , & mut enc ) . unwrap ( ) ; enc_copy = Some ( enc ) ; flash . erase ( offset , slot_len ) . unwrap ( ) ; } else { enc_copy = None ; } flash . write ( offset , & buf ) . unwrap ( ) ; let mut copy = vec ! [ 0u8 buf len ( ) ] ; flash . read ( offset , & mut copy ) . unwrap ( ) ; result = [ Some ( copy ) , enc_copy ] ; } else { flash . write ( offset , & buf ) . unwrap ( ) ; let mut copy = vec ! [ 0u8 buf len ( ) ] ; flash . read ( offset , & mut copy ) . unwrap ( ) ; let enc_copy : Option < Vec < u8 > > ; if is_encrypted { flash . erase ( offset , slot_len ) . unwrap ( ) ; flash . write ( offset , & encbuf ) . unwrap ( ) ; let mut enc = vec ! [ 0u8 encbuf len ( ) ] ; flash . read ( offset , & mut enc ) . unwrap ( ) ; enc_copy = Some ( enc ) ; } else { enc_copy = None ; } result = [ Some ( copy ) , enc_copy ] ; } result }
Install a "program" into the given image.
Install a " program " into the given image .
flashmap
&mut SimFlashMap
slots
&[SlotInfo]
slot
usize
len
usize
bad_sig
bool