mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-08 04:56:43 +00:00
tools: read-tags: fix bad pointer arithmetic
This removes raw pointer usage that could cause unaligned accesses. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
7da67f050a
commit
973945fce2
1 changed files with 26 additions and 9 deletions
|
|
@ -6,29 +6,46 @@ use std::io::prelude::*;
|
|||
use std::path::Path;
|
||||
use std::process;
|
||||
use std::slice;
|
||||
// use tools::make_type;
|
||||
|
||||
fn read_u32_from_ptr(p: *const u8) -> u32 {
|
||||
let sl = unsafe { slice::from_raw_parts(p, 4) };
|
||||
let mut arr: [u8; 4] = Default::default();
|
||||
arr.copy_from_slice(sl);
|
||||
u32::from_le_bytes(arr)
|
||||
}
|
||||
|
||||
fn read_u16_from_ptr(p: *const u8) -> u16 {
|
||||
let sl = unsafe { slice::from_raw_parts(p, 2) };
|
||||
let mut arr: [u8; 2] = Default::default();
|
||||
arr.copy_from_slice(sl);
|
||||
u16::from_le_bytes(arr)
|
||||
}
|
||||
|
||||
fn read_next_tag(b8: *mut u8, byte_offset: &mut usize) -> Result<(u32, u16, u32), ()> {
|
||||
let tag_name = u32::from_le(unsafe { (b8 as *mut u32).add(*byte_offset / 4).read() }) as u32;
|
||||
let tag_name = read_u32_from_ptr(b8.wrapping_add(*byte_offset));
|
||||
*byte_offset += 4;
|
||||
let crc = u16::from_le(unsafe { (b8 as *mut u16).add(*byte_offset / 2).read() }) as u16;
|
||||
|
||||
let crc = read_u16_from_ptr(b8.wrapping_add(*byte_offset));
|
||||
*byte_offset += 2;
|
||||
let size = u16::from_le(unsafe { (b8 as *mut u16).add(*byte_offset / 2).read() }) as u32 * 4;
|
||||
|
||||
let size = read_u16_from_ptr(b8.wrapping_add(*byte_offset)) as u32 * 4;
|
||||
*byte_offset += 2;
|
||||
|
||||
Ok((tag_name, crc, size))
|
||||
}
|
||||
|
||||
fn print_tag(b8: *mut u8, size: u32, crc: u16, byte_offset: &mut usize) -> Result<(), ()> {
|
||||
let data = unsafe { slice::from_raw_parts(b8.add(*byte_offset) as *const u8, size as usize) };
|
||||
let data_32 =
|
||||
unsafe { slice::from_raw_parts(b8.add(*byte_offset) as *const u32, size as usize / 4) };
|
||||
*byte_offset += size as usize;
|
||||
|
||||
let mut digest = crc16::Digest::new(crc16::X25);
|
||||
digest.write(data);
|
||||
|
||||
for byte in data_32 {
|
||||
print!(" {:08x}", byte);
|
||||
let mut word_arr: [u8; 4] = Default::default();
|
||||
for bytes in data.chunks(4) {
|
||||
word_arr.copy_from_slice(bytes);
|
||||
let word = u32::from_le_bytes(word_arr);
|
||||
print!(" {:08x}", word);
|
||||
}
|
||||
|
||||
if digest.sum16() == crc {
|
||||
|
|
@ -47,7 +64,7 @@ fn process_tags(b8: *mut u8) {
|
|||
let (tag_name, crc, size) =
|
||||
read_next_tag(b8, &mut byte_offset).expect("couldn't read next tag");
|
||||
if tag_name == u32::from_le_bytes(*b"XArg") && size == 20 {
|
||||
total_words = unsafe { (b8 as *mut u32).add(byte_offset / 4).read() } * 4;
|
||||
total_words = read_u32_from_ptr(b8.wrapping_add(byte_offset)) * 4;
|
||||
println!(
|
||||
"Found Xous Args Size at offset {}, setting total_words to {}",
|
||||
byte_offset, total_words
|
||||
|
|
|
|||
Loading…
Reference in a new issue