summaryrefslogtreecommitdiffstats
path: root/util/flashrom_tester/flashrom/src/lib.rs
blob: 41393e8413fca9b4a41d90aef4ce22cc3f473610 (plain)
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
//
// Copyright 2019, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Alternatively, this software may be distributed under the terms of the
// GNU General Public License ("GPL") version 2 as published by the Free
// Software Foundation.
//

#[macro_use]
extern crate log;

mod cmd;
mod flashromlib;

use std::{error, fmt, path::Path};

pub use cmd::FlashromCmd;
pub use flashromlib::FlashromLib;

pub use libflashrom::{
    flashrom_log_level, FLASHROM_MSG_DEBUG, FLASHROM_MSG_DEBUG2, FLASHROM_MSG_ERROR,
    FLASHROM_MSG_INFO, FLASHROM_MSG_SPEW, FLASHROM_MSG_WARN,
};

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum FlashChip {
    HOST,
}

impl FlashChip {
    pub fn from(s: &str) -> Result<FlashChip, &str> {
        match s {
            "host" => Ok(FlashChip::HOST),
            _ => Err("cannot convert str to enum"),
        }
    }
    pub fn to(fc: FlashChip) -> &'static str {
        match fc {
            FlashChip::HOST => "host",
        }
    }

    /// Return the programmer string and optional programmer options
    pub fn to_split(fc: FlashChip) -> (&'static str, Option<&'static str>) {
        let programmer = FlashChip::to(fc);
        let mut bits = programmer.splitn(2, ':');
        (bits.next().unwrap(), bits.next())
    }

    /// Return whether the hardware write protect signal can be controlled.
    ///
    /// Servo and dediprog adapters are assumed to always have hardware write protect
    /// disabled.
    pub fn can_control_hw_wp(&self) -> bool {
        match self {
            FlashChip::HOST => true,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct FlashromError {
    msg: String,
}

impl fmt::Display for FlashromError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}

impl error::Error for FlashromError {}

impl<T> From<T> for FlashromError
where
    T: Into<String>,
{
    fn from(msg: T) -> Self {
        FlashromError { msg: msg.into() }
    }
}

pub trait Flashrom {
    /// Returns the size of the flash in bytes.
    fn get_size(&self) -> Result<i64, FlashromError>;

    /// Returns the vendor name and the flash name.
    fn name(&self) -> Result<(String, String), FlashromError>;

    /// Set write protect status and range.
    fn wp_range(&self, range: (i64, i64), wp_enable: bool) -> Result<bool, FlashromError>;

    /// Read the write protect regions for the flash.
    fn wp_list(&self) -> Result<String, FlashromError>;

    /// Return true if the flash write protect status matches `en`.
    fn wp_status(&self, en: bool) -> Result<bool, FlashromError>;

    /// Set write protect status.
    /// If en=true sets wp_range to the whole chip (0,getsize()).
    /// If en=false sets wp_range to (0,0).
    /// This is due to the MTD driver, which requires wp enable to use a range
    /// length != 0 and wp disable to have the range 0,0.
    fn wp_toggle(&self, en: bool) -> Result<bool, FlashromError>;

    /// Read the whole flash to the file specified by `path`.
    fn read_into_file(&self, path: &Path) -> Result<(), FlashromError>;

    /// Read only a region of the flash into the file specified by `path`. Note
    /// the first byte written to the file is the first byte from the region.
    fn read_region_into_file(&self, path: &Path, region: &str) -> Result<(), FlashromError>;

    /// Write the whole flash to the file specified by `path`.
    fn write_from_file(&self, path: &Path) -> Result<(), FlashromError>;

    /// Write only a region of the flash.
    /// `path` is a file of the size of the whole flash.
    /// The `region` name corresponds to a region name in the `layout` file, not the flash.
    fn write_from_file_region(
        &self,
        path: &Path,
        region: &str,
        layout: &Path,
    ) -> Result<bool, FlashromError>;

    /// Verify the whole flash against the file specified by `path`.
    fn verify_from_file(&self, path: &Path) -> Result<(), FlashromError>;

    /// Verify only the region against the file specified by `path`.
    /// Note the first byte in the file is matched against the first byte of the region.
    fn verify_region_from_file(&self, path: &Path, region: &str) -> Result<(), FlashromError>;

    /// Erase the whole flash.
    fn erase(&self) -> Result<(), FlashromError>;

    /// Return true if the hardware write protect of this flash can be controlled.
    fn can_control_hw_wp(&self) -> bool;
}