fix serial code

This commit is contained in:
Argon 2024-02-27 13:44:20 +08:00
parent cbc1b33934
commit 3101876a02
1 changed files with 12 additions and 22 deletions

View File

@ -55,42 +55,28 @@ fn verify_code(codes : &Vec<(u32, u32)>) -> Result<(), String> {
let op_code = (first_code >> 28) as u8; let op_code = (first_code >> 28) as u8;
match op_code { match op_code {
0x0 | 0x1 |0x2 => { 0x0 | 0x1 |0x2 => {
if search_code {
if first_code & 0xf000000 != 0x8000000 {
return Err(format!("wrong code at line {}: need search code after search", line));
}
search_code = false
}
if pos + 2 > max_len { if pos + 2 > max_len {
return Err(format!("wrong code at line {}: unexpected end", line)); return Err(format!("wrong code at line {}: unexpected end", line));
} }
pos += 2; pos += 2;
} }
0x4 => { 0x4 => {
if search_code {
if first_code & 0xf000000 != 0x8000000 {
return Err(format!("wrong code at line {}: need search code after search", line));
}
search_code = false
}
if pos + 4 > max_len { if pos + 4 > max_len {
return Err(format!("wrong code at line {}: unexpected end", line)); return Err(format!("wrong code at line {}: unexpected end", line));
} }
let next_code = codes[pos + 2].1;
if next_code & 0xf0000000 != 0x40000000 {
return Err(format!("serial code error at line {}: should start with 0x4", line + 1));
}
pos += 4; pos += 4;
} }
0x5 => { 0x5 => {
if search_code {
return Err(format!("wrong code at line {}: need search code after search", line));
}
if pos + 4 > max_len { if pos + 4 > max_len {
return Err(format!("wrong code at line {}: unexpected end", line)); return Err(format!("wrong code at line {}: unexpected end", line));
} }
pos += 4; pos += 4;
} }
0x8 => { 0x8 => {
if search_code {
return Err(format!("wrong code at line {}: need search code after search", line));
}
let search_bytes = (first_code & 0xffff) as usize; let search_bytes = (first_code & 0xffff) as usize;
if search_bytes == 0 { if search_bytes == 0 {
return Err(format!("wrong code at line {}: search length should not be 0", line)); return Err(format!("wrong code at line {}: search length should not be 0", line));
@ -104,9 +90,6 @@ fn verify_code(codes : &Vec<(u32, u32)>) -> Result<(), String> {
pos += code_len; pos += code_len;
} }
0xa => { 0xa => {
if search_code {
return Err(format!("wrong code at line {}: need search code after search", line));
}
if pos + 1 >= max_len { if pos + 1 >= max_len {
return Err(format!("wrong code at line {}: unexpected end", line)); return Err(format!("wrong code at line {}: unexpected end", line));
} }
@ -204,7 +187,14 @@ fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> Result<(), String>
0x4 => { 0x4 => {
code_len = 4; code_len = 4;
let mut val = codes[pos + 1].1; let mut val = codes[pos + 1].1;
let mut addr = (first_code & 0xffffff) as usize; let mut addr = if (first_code & 0xf000000) >= 0x8000000 {
if search_addr == None {
continue;
}
(first_code & 0xffffff) as usize + search_addr.unwrap()
} else {
(first_code & 0xffffff) as usize
};
let mut count = (codes[pos + 2].1 & 0xfff0000) >> 16; let mut count = (codes[pos + 2].1 & 0xfff0000) >> 16;
let addr_diff = codes[pos + 2].1 & 0xffff; let addr_diff = codes[pos + 2].1 & 0xffff;
let val_diff = codes[pos + 3].1; let val_diff = codes[pos + 3].1;