|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <string.h> |
| 6 | +#include <strings.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdbool.h> |
| 9 | + |
| 10 | +#include <sys/types.h> |
| 11 | +#include <fcntl.h> |
| 12 | + |
| 13 | +#include <pcap.h> |
| 14 | + |
| 15 | +struct usb_pkt { |
| 16 | + uint8_t ignored1[16]; |
| 17 | + uint8_t incoming; |
| 18 | + uint8_t ignored2[6]; |
| 19 | + uint32_t length; |
| 20 | + uint8_t data[0]; |
| 21 | +} __attribute__((__packed__)); |
| 22 | + |
| 23 | +static int get_next_pkt(pcap_t *pcap, struct usb_pkt **pkt) { |
| 24 | + int ret; |
| 25 | + struct pcap_pkthdr *header; |
| 26 | + |
| 27 | + do { |
| 28 | + ret = pcap_next_ex(pcap, &header, (const uint8_t **)pkt); |
| 29 | + if (ret != 1) |
| 30 | + break; |
| 31 | + } while (((*pkt)->incoming)); |
| 32 | + |
| 33 | + return (ret == 1); |
| 34 | +} |
| 35 | + |
| 36 | +int main(int argc, char **argv) { |
| 37 | + char *filename; |
| 38 | + char pcap_err[PCAP_ERRBUF_SIZE]; |
| 39 | + pcap_t *pcap; |
| 40 | + struct usb_pkt *pkt; |
| 41 | + uint32_t pkt_index = 0; |
| 42 | + |
| 43 | + if (argc != 2) { |
| 44 | + printf("Usage: %s [dumpfile.pcap]\n", argv[0]); |
| 45 | + return 1; |
| 46 | + } |
| 47 | + |
| 48 | + filename = argv[1]; |
| 49 | + pcap = pcap_open_offline(filename, pcap_err); |
| 50 | + |
| 51 | + if (!pcap) { |
| 52 | + fprintf(stderr, "Unable to open pcap file: %s\n", pcap_err); |
| 53 | + return 1; |
| 54 | + } |
| 55 | + |
| 56 | + /* Seek to offset 598, which is where command 0xd7 starts */ |
| 57 | + while (get_next_pkt(pcap, &pkt)) { |
| 58 | + pkt_index++; |
| 59 | + |
| 60 | + if ((pkt->length == 1) && (pkt->data[0] == 0xd7)) { |
| 61 | + uint32_t addr; |
| 62 | + uint32_t bytes; |
| 63 | + uint32_t sig_bytes; |
| 64 | + printf("Found download at offset %d\n", pkt_index); |
| 65 | + |
| 66 | + get_next_pkt(pcap, &pkt); |
| 67 | + memcpy(&addr, pkt->data, 4); |
| 68 | + printf("Writing to address 0x%08x\n", be32toh(addr)); |
| 69 | + |
| 70 | + get_next_pkt(pcap, &pkt); |
| 71 | + memcpy(&bytes, pkt->data, 4); |
| 72 | + printf("Writing %d bytes\n", be32toh(bytes)); |
| 73 | + |
| 74 | + get_next_pkt(pcap, &pkt); |
| 75 | + memcpy(&sig_bytes, pkt->data, 4); |
| 76 | + printf("Signature is %d bytes\n", be32toh(sig_bytes)); |
| 77 | + |
| 78 | + printf("\n"); |
| 79 | + |
| 80 | + uint8_t data[be32toh(bytes)]; |
| 81 | + uint32_t copied = 0; |
| 82 | + while (copied < be32toh(bytes)) { |
| 83 | + int ret; |
| 84 | + ret = get_next_pkt(pcap, &pkt); |
| 85 | + if (!ret) { |
| 86 | + fprintf(stderr, "Packet cut short\n"); |
| 87 | + break; |
| 88 | + } |
| 89 | + memcpy(data + copied, pkt->data, pkt->length); |
| 90 | + copied += pkt->length; |
| 91 | + } |
| 92 | + |
| 93 | + char name[32]; |
| 94 | + snprintf(name, sizeof(name) - 1, "prog-0x%08x", be32toh(addr)); |
| 95 | + int fd = open(name, O_WRONLY | O_CREAT, 0777); |
| 96 | + if (fd == -1) { |
| 97 | + perror("Unable to open file for writing"); |
| 98 | + return 1; |
| 99 | + } |
| 100 | + write(fd, data, sizeof(data)); |
| 101 | + close(fd); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return 0; |
| 106 | +} |
0 commit comments