diff options
author | Amaury Pouly <amaury.pouly@gmail.com> | 2014-06-24 18:04:17 +0200 |
---|---|---|
committer | Amaury Pouly <amaury.pouly@gmail.com> | 2014-06-24 18:07:56 +0200 |
commit | c9a028cc183d638c16ca9a8858b783b1830be16f (patch) | |
tree | 1bd29843ec0b8d3eb80a6209d1c4e32144175dfe /utils/hwpatcher/view.lua | |
parent | 761f59c9e3be0ffd77d2dc1b8095a3b877badeda (diff) | |
download | rockbox-c9a028cc183d638c16ca9a8858b783b1830be16f.tar.gz rockbox-c9a028cc183d638c16ca9a8858b783b1830be16f.zip |
Introduce hwpatcher, a tool to patch binaries
This tool is a scriptable (lua) tool to patch binaries, it supports:
- raw binary
- ELF
- SB(v1/v2)
It also contains some basic routines to parse and generate useful arm/thumb code
like jump or register load/store. This is very useful to take a firmware and
patch an interrupt vector or some code to jump to an extra payload added to
the binary. Examples are provided for several STMP based target which the payload
is expected to be hwstub, and also for the Sansa View. A typical patcher usually
requires three elements:
- the lua patcher itself
- the payload (hwstub for example)
- (optional) a small stub either to jump properly to the payload or determine
under which circumstance to do the jump (hold a key for example)
Change-Id: I6d36020a3bc9e636615ac8221b7591ade5f251e3
Diffstat (limited to 'utils/hwpatcher/view.lua')
-rw-r--r-- | utils/hwpatcher/view.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/utils/hwpatcher/view.lua b/utils/hwpatcher/view.lua new file mode 100644 index 0000000000..35c383800f --- /dev/null +++ b/utils/hwpatcher/view.lua | |||
@@ -0,0 +1,38 @@ | |||
1 | --[[ | ||
2 | Sansa View bootloader hacking | ||
3 | required argument (in order): | ||
4 | - path to bootloader | ||
5 | - path to output bootloader | ||
6 | - path to stub | ||
7 | ]]-- | ||
8 | require("lib") | ||
9 | require("arm") | ||
10 | |||
11 | if #arg < 3 then | ||
12 | error("not enough argument to fuzep patcher") | ||
13 | end | ||
14 | |||
15 | local md5 = hwp.md5sum(arg[1]) | ||
16 | if hwp.md5str(md5) ~= "4bc1760327c37b9ffd00315c8aa7f376" then | ||
17 | error("MD5 sum of the file doesn't match") | ||
18 | end | ||
19 | |||
20 | local fw = hwp.load_file(arg[1]) | ||
21 | local jump_instr_addr = arm.to_thumb(hwp.make_addr(0x753C)) | ||
22 | local stub_addr = hwp.make_addr(0x137B0) | ||
23 | -- read old jump address | ||
24 | --local old_jump = arm.parse_branch(fw, jump_instr_addr) | ||
25 | --print(string.format("Old jump address: %s", old_jump)) | ||
26 | -- put stub at the right place | ||
27 | local stub = hwp.load_bin_file(arg[3]) | ||
28 | local stub_info = hwp.section_info(stub, "") | ||
29 | local stub_data = hwp.read(stub, hwp.make_addr(stub_info.addr, ""), stub_info.size) | ||
30 | hwp.write(fw, stub_addr, stub_data) | ||
31 | -- patch jump | ||
32 | local branch_to_stub = arm.make_branch(arm.to_arm(stub_addr), true) | ||
33 | arm.write_branch(fw, jump_instr_addr, branch_to_stub, hwp.inc_addr(stub_addr, stub_info.size)) | ||
34 | -- read jump address | ||
35 | local new_jump = arm.parse_branch(fw, jump_instr_addr) | ||
36 | print(string.format("New jump address: %s", new_jump)) | ||
37 | -- save | ||
38 | hwp.save_file(fw, arg[2]) | ||