-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmotoxtract.c
109 lines (94 loc) · 3.19 KB
/
motoxtract.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* motoxtract.c
*
* Copyright (c) Ramsudharsan <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define SECTOR_SIZE 512
#define HEADER_SIZE 1024
#include "sha256.h"
int usage(char *name) {
printf("usage:\t%s <path_to_motoboot.img_file>\n", name);
return 0;
}
struct entry {
char partitionName[24];
uint32_t firstSector;
uint32_t lastSector;
};
struct moto_header {
uint32_t partitionNumber;
struct entry partitions[30];
char padding[28];
}header;
int main (int argc, char **argv) {
FILE *motoboot, *partitionDump;
unsigned char buff[HEADER_SIZE];
int partitionCount, i, j, k;
struct entry *curHdr = 0;
uint8_t curSha[32] = {0};
SHA256_CTX ctx = {0};
char partitionName[32];
if (argc != 2)
return usage(argv[0]);
motoboot = fopen(argv[1], "r");
if (motoboot == NULL) {
printf("unable to open %s file, abort\n", argv[1]);
return -1;
}
printf("reading file %s\n", argv[1]);
fread((void*)buff, sizeof(buff), 1, motoboot);
memcpy(&header, buff, sizeof(header));
partitionCount = header.partitionNumber;
if (partitionCount > 30) {
printf("error reading the file, too many partitions\n");
return -1;
}
printf("found %d partitions\n", partitionCount);
for (i = 0; i < partitionCount; i++) {
curHdr = &header.partitions[i];
snprintf(partitionName, sizeof(partitionName), "%s_%#x_%#x.mbn", curHdr->partitionName, curHdr->firstSector, curHdr->lastSector);
partitionDump = fopen(partitionName, "w+");
if (partitionDump == NULL) {
printf("unable to write to file %s", partitionName);
return -1;
}
memset(&ctx, 0, sizeof(ctx));
sha256_init(&ctx);
uint32_t sectors = curHdr->lastSector - curHdr->firstSector + 1;
printf("dumping partition %d (%s), starting offset in file 0x%08X\n", i, curHdr->partitionName, HEADER_SIZE + curHdr->firstSector * SECTOR_SIZE);
for (j = 0; j < sectors; j++) {
fread((void*)buff, SECTOR_SIZE, 1, motoboot);
sha256_update(&ctx, buff, SECTOR_SIZE);
fwrite((void*)buff, SECTOR_SIZE, 1, partitionDump);
}
sha256_final(&ctx, curSha);
printf("\tSHA256: ");
for (k = 0; k < sizeof(curSha);k++)
{
printf("%02x", curSha[k]);
}
printf("\n");
fclose(partitionDump);
}
fclose(motoboot);
return 0;
}