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
use Section;
use byteorder::{LittleEndian, ReadBytesExt};
use quick_error::ResultExt;
use std::io::{Error as IoError, Read, Seek, SeekFrom};
pub const MAGIC_STR: &'static str = "ANDROID!";
const MAGIC_SIZE: usize = 8;
const PRODUCT_NAME_SIZE: usize = 24;
const BOOT_ARGUMENTS_SIZE: usize = 512;
const UNIQUE_ID_SIZE: usize = 32;
const HEADER_SIZE: usize = 616;
const SECTIONS: &'static [Section] = &[
Section::Header,
Section::Kernel,
Section::Ramdisk,
Section::Second,
Section::DeviceTree,
];
#[derive(Debug)]
pub struct MagicHeader {
pub magic: [u8; MAGIC_SIZE],
pub kernel_size: u32,
pub kernel_load_address: u32,
pub ramdisk_size: u32,
pub ramdisk_load_address: u32,
pub second_size: u32,
pub second_load_address: u32,
pub device_tree_size: u32,
_reserved: u32,
pub kernel_tags_address: u32,
pub page_size: u32,
pub product_name: [u8; PRODUCT_NAME_SIZE],
pub boot_arguments: [[u8; BOOT_ARGUMENTS_SIZE / 16]; 16],
pub unique_id: [u8; UNIQUE_ID_SIZE],
}
impl MagicHeader {
pub fn read_from<R: ReadBytesExt>(
source: &mut R,
check_magic: bool,
) -> Result<Self, MagicHeaderParseError> {
let header = MagicHeader {
magic: {
let mut buffer = [0; MAGIC_SIZE];
source.read_exact(&mut buffer)?;
buffer
},
kernel_size: source.read_u32::<LittleEndian>()?,
kernel_load_address: source.read_u32::<LittleEndian>()?,
ramdisk_size: source.read_u32::<LittleEndian>()?,
ramdisk_load_address: source.read_u32::<LittleEndian>()?,
second_size: source.read_u32::<LittleEndian>()?,
second_load_address: source.read_u32::<LittleEndian>()?,
device_tree_size: source.read_u32::<LittleEndian>()?,
_reserved: source.read_u32::<LittleEndian>()?,
kernel_tags_address: source.read_u32::<LittleEndian>()?,
page_size: source.read_u32::<LittleEndian>()?,
product_name: {
let mut buffer = [0; PRODUCT_NAME_SIZE];
source.read_exact(&mut buffer)?;
buffer
},
boot_arguments: unsafe {
use std::mem::transmute;
let mut buffer = [0; BOOT_ARGUMENTS_SIZE];
source.read_exact(&mut buffer)?;
transmute(buffer)
},
unique_id: {
let mut buffer = [0u8; UNIQUE_ID_SIZE];
source.read_exact(&mut buffer)?;
buffer
},
};
if check_magic && header.magic != MAGIC_STR.as_bytes() {
Err(MagicHeaderParseError::InvalidMagic(header))
} else {
Ok(header)
}
}
pub fn section_size(&self, section: Section) -> u64 {
match section {
Section::Header => HEADER_SIZE as u64,
Section::Kernel => self.kernel_size as u64,
Section::Ramdisk => self.ramdisk_size as u64,
Section::Second => self.second_size as u64,
Section::DeviceTree => self.device_tree_size as u64,
}
}
pub fn section_start(&self, section: Section) -> Result<u64, LocateSectionError> {
if self.page_size == 0 {
Err(LocateSectionError::NoPageSize)
} else {
let offset_in_pages: u64 = SECTIONS
.iter()
.cloned()
.take_while(|&i_section| i_section != section)
.map(|section| {
(self.section_size(section) + self.page_size as u64 - 1) / self.page_size as u64
})
.sum();
Ok(offset_in_pages * self.page_size as u64)
}
}
pub fn section_location(&self, section: Section) -> Result<(u64, u64), LocateSectionError> {
Ok((self.section_start(section)?, self.section_size(section)))
}
pub fn read_section_from<R: Read + Seek>(
&self,
source: &mut R,
section: Section,
) -> Result<Vec<u8>, ReadSectionError> {
let (start, size) = self.section_location(section).context(section)?;
try!(source.seek(SeekFrom::Start(start)).context(section));
let mut data = vec![0u8; size as usize];
try!(source.read_exact(&mut data).context(section));
return Ok(data);
}
pub fn sections(&self) -> Vec<Section> {
SECTIONS
.iter()
.filter(|&§ion| self.section_size(section) > 0)
.cloned()
.collect()
}
}
quick_error! {
#[derive(Debug)]
pub enum MagicHeaderParseError {
Io(error: IoError) {
description("I/O error while parsing header")
display("I/O error while parsing header.")
cause(error)
from(error: IoError) -> (error)
}
InvalidMagic(header: MagicHeader) {
description("The header did not have the valid magic prefix")
display("The header did not have the valid '{}' magic prefix.", MAGIC_STR)
}
}
}
quick_error! {
#[derive(Debug)]
pub enum LocateSectionError {
NoPageSize {
description("The header's page size is zero")
display("The header's page size is 0.")
}
NoSection(section: Section) {
description("The section does not exist")
display("The '{}' section does not exist.", section)
}
}
}
quick_error! {
#[derive(Debug)]
pub enum ReadSectionError {
LocateSection(section: Section, cause: LocateSectionError) {
context(section: Section, cause: LocateSectionError) -> (section, cause)
description("Could not locate the section")
display("Cannot locate the '{}' section.", section)
cause(cause)
}
IoError(section: Section, cause: IoError) {
context(section: Section, cause: IoError) -> (section, cause)
description("I/O error while reading the section")
display("I/O error while reading the '{}' section.", section)
cause(cause)
}
}
}