1 /*
2 * Copyright 2016, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <stdio.h>
20
21 #include "disassembler.h"
22 #include "next/apf_defs.h"
23 #include "next/apf.h"
24
25 // Disassembles an APF program. A hex dump of the program is supplied on stdin.
26 //
27 // NOTE: This is a simple debugging tool not meant for shipping or production use. It is by no
28 // means hardened against malicious input and contains known vulnerabilities.
29 //
30 // Example usage:
31 // adb shell dumpsys wifi ipmanager | sed '/Last program:/,+1!d;/Last program:/d;s/[ ]*//' | out/host/linux-x86/bin/apf_disassembler
main(void)32 int main(void) {
33 uint32_t program_len = 0;
34 uint8_t program[10000];
35
36 // Read in hex program bytes
37 int byte;
38 while (scanf("%2x", &byte) == 1 && program_len < sizeof(program)) {
39 program[program_len++] = byte;
40 }
41
42 const u8 v6_marker = JMP_OPCODE << 3 | 1;
43 const bool is_v6 = (program[0] & 0b11111001) == v6_marker;
44 if (is_v6) {
45 printf("APFv6 program:\n");
46 } else {
47 printf("APFv4 program:\n");
48 }
49 for (uint32_t pc = 0; pc < program_len + 2;) {
50 const disas_ret ret = apf_disassemble(program, program_len, &pc, is_v6);
51 printf("%s%s\n", ret.prefix, ret.content);
52 }
53 }
54