1pub const MAGIC: u32 = 0x444d_4b48;
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum Error {
16 Bounds,
18 Kind,
20 Window,
22 Jump,
24}
25
26pub struct Package<'a> {
28 pub id: u32,
30 pub image: &'a [u32],
32 pub fixups: &'a [u32],
34 pub code_end: u32,
36}
37impl<'a> Package<'a> {
38 pub fn parse(words: &'a [u32]) -> Result<(Self, &'a [u32]), Error> {
41 let header = words.get(..5).ok_or(Error::Bounds)?;
42 if header[0] != MAGIC {
43 return Err(Error::Bounds);
44 }
45 let image_end = 5usize
46 .checked_add(header[2] as usize)
47 .ok_or(Error::Bounds)?;
48 let end = image_end
49 .checked_add(header[3] as usize)
50 .ok_or(Error::Bounds)?;
51 let image = words.get(5..image_end).ok_or(Error::Bounds)?;
52 let fixups = words.get(image_end..end).ok_or(Error::Bounds)?;
53 let bytes = image.len().checked_mul(4).ok_or(Error::Bounds)?;
54 if header[4] as usize > bytes || header[4] & 3 != 0 {
55 return Err(Error::Bounds);
56 }
57 for &fixup in fixups {
58 let offset = fixup & 0x0fff_ffff;
59 if offset & 3 != 0 || offset as usize >= bytes {
60 return Err(Error::Bounds);
61 }
62 if !(1..=4).contains(&(fixup >> 28)) {
63 return Err(Error::Kind);
64 }
65 }
66 Ok((
67 Self {
68 id: header[1],
69 image,
70 fixups,
71 code_end: header[4],
72 },
73 &words[end..],
74 ))
75 }
76}
77
78pub const fn window(address: u32) -> u32 {
80 address.wrapping_add(0x8000) & 0xffff_0000
81}
82
83pub fn placement(address: u32, bytes: u32, code: bool) -> Result<(), Error> {
85 let last = address
86 .checked_add(bytes.checked_sub(1).ok_or(Error::Bounds)?)
87 .ok_or(Error::Bounds)?;
88 if address & 3 != 0 || bytes & 3 != 0 {
89 return Err(Error::Bounds);
90 }
91 if code && window(address) != window(last) {
92 return Err(Error::Window);
93 }
94 Ok(())
95}
96
97pub fn direct(word: u32, kind: u32, target: u32, site: u32) -> Result<u32, Error> {
100 Ok(match kind {
101 1 => {
102 if target & 3 != 0 || target & 0xf000_0000 != site.wrapping_add(4) & 0xf000_0000 {
103 return Err(Error::Jump);
104 }
105 (word & 0xfc00_0000) | ((target >> 2) & 0x03ff_ffff)
106 }
107 2 => (word & 0xffff_0000) | (window(target) >> 16),
108 3 => (word & 0xffff_0000) | (target.wrapping_sub(window(target)) & 0xffff),
109 4 => target,
110 _ => return Err(Error::Kind),
111 })
112}
113
114pub fn relocate(word: u32, kind: u32, linked: u32, loaded: u32, offset: u32) -> Result<u32, Error> {
117 let delta = loaded.wrapping_sub(linked);
118 Ok(match kind {
119 1 => {
120 let source = linked.checked_add(offset).ok_or(Error::Bounds)?;
121 let target = (source.wrapping_add(4) & 0xf000_0000) | ((word & 0x03ff_ffff) << 2);
122 direct(
123 word,
124 1,
125 target.wrapping_add(delta),
126 loaded.checked_add(offset).ok_or(Error::Bounds)?,
127 )?
128 }
129 2 => (word & 0xffff_0000) | (window(loaded) >> 16),
130 3 => {
131 let offset =
132 ((word as u16 as i16) as i32).wrapping_sub((linked as u16 as i16) as i32) as u32;
133 (word & 0xffff_0000)
134 | (loaded.wrapping_add(offset).wrapping_sub(window(loaded)) & 0xffff)
135 }
136 4 => word.wrapping_add(delta),
137 _ => return Err(Error::Kind),
138 })
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144 #[test]
145 fn package_bounds_empty_data_and_concatenation() {
146 let words = [
147 MAGIC,
148 3,
149 1,
150 1,
151 4,
152 0x0800_0000,
153 0x1000_0000,
154 MAGIC,
155 4,
156 0,
157 0,
158 0,
159 ];
160 let (p, next) = Package::parse(&words).unwrap();
161 assert_eq!(p.id, 3);
162 assert_eq!(p.code_end, 4);
163 assert!(Package::parse(next).unwrap().0.image.is_empty());
164 for bad in [0x1000_0004, 0x1000_0001, 0x5000_0000] {
165 let mut invalid = words;
166 invalid[6] = bad;
167 assert!(Package::parse(&invalid).is_err());
168 }
169 assert!(Package::parse(&words[..6]).is_err());
170 assert!(Package::parse(&[MAGIC, 2, 1, 0, 0, 42]).is_ok());
171 }
172 #[test]
173 fn arbitrary_link_low_half_and_real_jump_pc() {
174 assert_eq!(
175 relocate(0x2421_1020, 3, 0x8040_1000, 0x801d_9000, 0).unwrap(),
176 0x2421_9020
177 );
178 assert_eq!(
179 relocate(0x3c01_8040, 2, 0x8040_1000, 0x801d_9000, 0).unwrap(),
180 0x3c01_801e
181 );
182 assert_eq!(
183 relocate(0x8040_1020, 4, 0x8040_1000, 0x801d_9000, 0).unwrap(),
184 0x801d_9020
185 );
186 assert_eq!(
187 relocate(0x0c00_0000, 1, 0x8fff_fffc, 0x801d_8000, 0).unwrap(),
188 direct(0x0c00_0000, 1, 0x801d_8004, 0x801d_8000).unwrap()
189 );
190 assert_eq!(
191 direct(0x0c00_0000, 1, 0x9000_0000, 0x8fff_fff8),
192 Err(Error::Jump)
193 );
194 }
195 #[test]
196 fn destination_window_and_arithmetic_bounds() {
197 assert_eq!(placement(0x801d_7ffc, 8, true), Err(Error::Window));
198 assert!(placement(0x801d_8000, 0x10000, true).is_ok());
199 assert!(placement(0xffff_fffc, 8, false).is_err());
200 assert!(placement(0x801d_8000, 0, true).is_err());
201 }
202}