-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
194 lines (168 loc) · 6.56 KB
/
index.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title>Android Boot Image</title>
<script src="KaitaiStream.js"></script>
<script src="AndroidImg.js"></script>
<style>
table {
border: 4px double black;
border-collapse: collapse;
font-family: monospace;
}
th {
text-align: left;
background: #ccc;
padding: 5px;
border: 1px solid black;
}
td {
padding: 3px;
border: 1px solid black;
}
</style>
</head>
<body>
<center>
<h1>Android Boot Image</h1>
</br>
<label for="file">Select boot image:</label>
<input type="file" id="files" name="file"/>
</br>
<div id="patch_block" style="display:none">
<label for="os_patch_level">Patch to new date:</label>
<input type="month" id="os_patch_level" name="os_patch_level" min="2000-01" max="2127-12" value="2020-01" pattern="2[01][0-9]{2}-[01][0-9]" onchange="enable_patch_button(event);"/>
<input type="button" id="patch" value="Patch" accesskey="p" disabled onclick="patch_image(event);"/>
</br>
</div>
<output id="info"></output>
</center>
<script>
var boot_img_file;
function patch_image(e) {
if (!is_valid_patch_level()) {
alert("Incorrect date.");
return;
}
let input_date = document.getElementById('os_patch_level').value;
let reader = new FileReader();
let url = window.URL || window.webkitURL;
reader.onloadend = function (e) {
if (e.target.readyState == FileReader.DONE) {
let data = new DataView(e.target.result);
let os_version = data.getUint32(44, 1);
let android_version = os_version >>> 11;
let year = ((os_version >>> 4) & 0x7f) + 2000;
let month = os_version & 0xf;
console.log(to_hex(os_version));
console.log(month);
console.log(year);
let new_year = input_date.substr(0, 4);
let new_month = input_date.substr(5, 2);
if (new_year == year && month == new_month) {
alert("Nothing to be done: os_patch_level is the same.");
return;
}
let new_os_version = (android_version << 11) | ((new_year - 2000) << 4) | new_month;
console.log(to_hex(new_os_version));
data.setUint32(44, new_os_version, 1);
let blob = new Blob([data], {type: "application/x-raw-disk-image"});
let link = document.createElement('a');
link.href = url.createObjectURL(blob);
let name = boot_img_file.name;
let sep = name.lastIndexOf('.');
let basename = name.substring(0, sep);
let ext = name.substring(sep);
link.download = basename + '_' + input_date + ext;
link.click();
delete link;
document.getElementById('patch').disabled = true;
}
};
reader.onerror = function (e) {
document.getElementById('info').innerHTML = 'Error patching the image: ' + e.target.error.code;
};
reader.readAsArrayBuffer(boot_img_file);
}
function is_valid_patch_level() {
if (document.getElementById('os_patch_level').checkValidity()) {
let input_date = document.getElementById('os_patch_level').value;
let year = input_date.substr(0, 4);
let month = input_date.substr(5, 2);
if (2000 <= year && year <= 2127 && 1 <= month && month <= 12) {
return true;
}
}
return false;
}
function enable_patch_button(e) {
if (is_valid_patch_level()) {
document.getElementById('patch').disabled = false;
}
}
function add_line(name, value) {
return '<tr><td>' + name + '</td><td>' + value + '</td></tr>';
}
function to_hex(num) {
return '0x' + num.toString(16).toUpperCase().padStart(8, '0');
}
function dump_boot_info(e) {
let reader = new FileReader();
if (e.target.files.length == 0) {
document.getElementById('info').innerHTML = '';
document.getElementById('patch_block').style.display = 'none';
document.getElementById('patch').disabled = true;
boot_img_file = undefined;
return;
}
boot_img_file = e.target.files[0];
reader.onloadend = function (e) {
if (e.target.readyState == FileReader.DONE) {
let contents = e.target.result;
let output = [];
let img;
try {
img = new AndroidImg(new KaitaiStream(contents));
} catch (e) {
document.getElementById('info').innerHTML = 'Error analysing the image: ' + e;
return;
}
output.push(add_line('header_version', img.headerVersion));
let os_version = img.osVersion;
let os_patch_level = os_version.year + '-' + os_version.month.toString().padStart(2, '0');
output.push(add_line('os_version', os_version.major + '.' + os_version.minor + '.' + os_version.patch));
output.push(add_line('<b>os_patch_level</b>', '<b>' + os_patch_level + '</b>'));
if (img.name) {
output.push(add_line('board', img.name));
}
output.push(add_line('pagesize', img.pageSize));
if (img.cmdline) {
output.push(add_line('cmdline', img.cmdline));
}
if (img.extraCmdline) {
output.push(add_line('extra_cmdline', img.extraCmdline));
}
output.push(add_line('base', to_hex(img.base)));
output.push(add_line('kernel_offset', to_hex(img.kernelOffset)));
output.push(add_line('ramdisk_offset', to_hex(img.ramdiskOffset)));
output.push(add_line('second_offset', to_hex(img.secondOffset)));
output.push(add_line('tags_offset', to_hex(img.tagsOffset)));
document.getElementById('info').innerHTML = '<table><tr><th>mkbootimg</th><th>argument value</th></tr>' + output.join('') + '</table>';
document.getElementById('os_patch_level').value = os_patch_level;
document.getElementById('patch_block').style.display = 'inline';
}
};
reader.onerror = function (e) {
document.getElementById('info').innerHTML = 'Error analysing the image: ' + e.target.error.code;
};
const BOOT_IMAGE_HEADER_V1_SIZE = 1648;
const BOOT_IMAGE_HEADER_V2_SIZE = 1660;
reader.readAsArrayBuffer(boot_img_file.slice(0, BOOT_IMAGE_HEADER_V2_SIZE));
}
document.getElementById('files').addEventListener('change', dump_boot_info, false);
document.getElementById('patch').disabled = true;
</script>
</body>
</html>