-
Notifications
You must be signed in to change notification settings - Fork 3
/
AndroidImg.js
291 lines (269 loc) · 8.93 KB
/
AndroidImg.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['kaitai-struct/KaitaiStream'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('kaitai-struct/KaitaiStream'));
} else {
root.AndroidImg = factory(root.KaitaiStream);
}
}(this, function (KaitaiStream) {
/**
* @see {@link https://source.android.com/devices/bootloader/boot-image-header|Source}
*/
var AndroidImg = (function() {
function AndroidImg(_io, _parent, _root) {
this._io = _io;
this._parent = _parent;
this._root = _root || this;
this._read();
}
AndroidImg.prototype._read = function() {
this.magic = this._io.ensureFixedContents([65, 78, 68, 82, 79, 73, 68, 33]);
this.kernel = new Load(this._io, this, this._root);
this.ramdisk = new Load(this._io, this, this._root);
this.second = new Load(this._io, this, this._root);
this.tagsLoad = this._io.readU4le();
this.pageSize = this._io.readU4le();
this.headerVersion = this._io.readU4le();
this.osVersion = new OsVersion(this._io, this, this._root);
this.name = KaitaiStream.bytesToStr(KaitaiStream.bytesTerminate(this._io.readBytes(16), 0, false), "ASCII");
this.cmdline = KaitaiStream.bytesToStr(KaitaiStream.bytesTerminate(this._io.readBytes(512), 0, false), "ASCII");
this.sha = this._io.readBytes(32);
this.extraCmdline = KaitaiStream.bytesToStr(KaitaiStream.bytesTerminate(this._io.readBytes(1024), 0, false), "ASCII");
if (this.headerVersion > 0) {
this.recoveryDtbo = new SizeOffset(this._io, this, this._root);
}
if (this.headerVersion > 0) {
this.bootHeaderSize = this._io.readU4le();
}
if (this.headerVersion > 1) {
this.dtb = new LoadLong(this._io, this, this._root);
}
}
var Load = AndroidImg.Load = (function() {
function Load(_io, _parent, _root) {
this._io = _io;
this._parent = _parent;
this._root = _root || this;
this._read();
}
Load.prototype._read = function() {
this.size = this._io.readU4le();
this.addr = this._io.readU4le();
}
return Load;
})();
var LoadLong = AndroidImg.LoadLong = (function() {
function LoadLong(_io, _parent, _root) {
this._io = _io;
this._parent = _parent;
this._root = _root || this;
this._read();
}
LoadLong.prototype._read = function() {
this.size = this._io.readU4le();
this.addr = this._io.readU8le();
}
return LoadLong;
})();
var SizeOffset = AndroidImg.SizeOffset = (function() {
function SizeOffset(_io, _parent, _root) {
this._io = _io;
this._parent = _parent;
this._root = _root || this;
this._read();
}
SizeOffset.prototype._read = function() {
this.size = this._io.readU4le();
this.offset = this._io.readU8le();
}
return SizeOffset;
})();
var OsVersion = AndroidImg.OsVersion = (function() {
function OsVersion(_io, _parent, _root) {
this._io = _io;
this._parent = _parent;
this._root = _root || this;
this._read();
}
OsVersion.prototype._read = function() {
this.version = this._io.readU4le();
}
Object.defineProperty(OsVersion.prototype, 'month', {
get: function() {
if (this._m_month !== undefined)
return this._m_month;
this._m_month = (this.version & 15);
return this._m_month;
}
});
Object.defineProperty(OsVersion.prototype, 'patch', {
get: function() {
if (this._m_patch !== undefined)
return this._m_patch;
this._m_patch = ((this.version >>> 11) & 127);
return this._m_patch;
}
});
Object.defineProperty(OsVersion.prototype, 'year', {
get: function() {
if (this._m_year !== undefined)
return this._m_year;
this._m_year = (((this.version >>> 4) & 127) + 2000);
return this._m_year;
}
});
Object.defineProperty(OsVersion.prototype, 'major', {
get: function() {
if (this._m_major !== undefined)
return this._m_major;
this._m_major = ((this.version >>> 25) & 127);
return this._m_major;
}
});
Object.defineProperty(OsVersion.prototype, 'minor', {
get: function() {
if (this._m_minor !== undefined)
return this._m_minor;
this._m_minor = ((this.version >>> 18) & 127);
return this._m_minor;
}
});
return OsVersion;
})();
Object.defineProperty(AndroidImg.prototype, 'kernelImg', {
get: function() {
if (this._m_kernelImg !== undefined)
return this._m_kernelImg;
var _pos = this._io.pos;
this._io.seek(this.pageSize);
this._m_kernelImg = this._io.readBytes(this.kernel.size);
this._io.seek(_pos);
return this._m_kernelImg;
}
});
/**
* tags offset from base
*/
Object.defineProperty(AndroidImg.prototype, 'tagsOffset', {
get: function() {
if (this._m_tagsOffset !== undefined)
return this._m_tagsOffset;
this._m_tagsOffset = (this.tagsLoad - this.base);
return this._m_tagsOffset;
}
});
/**
* ramdisk offset from base
*/
Object.defineProperty(AndroidImg.prototype, 'ramdiskOffset', {
get: function() {
if (this._m_ramdiskOffset !== undefined)
return this._m_ramdiskOffset;
this._m_ramdiskOffset = (this.ramdisk.addr > 0 ? (this.ramdisk.addr - this.base) : 0);
return this._m_ramdiskOffset;
}
});
/**
* 2nd bootloader offset from base
*/
Object.defineProperty(AndroidImg.prototype, 'secondOffset', {
get: function() {
if (this._m_secondOffset !== undefined)
return this._m_secondOffset;
this._m_secondOffset = (this.second.addr > 0 ? (this.second.addr - this.base) : 0);
return this._m_secondOffset;
}
});
/**
* kernel offset from base
*/
Object.defineProperty(AndroidImg.prototype, 'kernelOffset', {
get: function() {
if (this._m_kernelOffset !== undefined)
return this._m_kernelOffset;
this._m_kernelOffset = (this.kernel.addr - this.base);
return this._m_kernelOffset;
}
});
/**
* dtb offset from base
*/
Object.defineProperty(AndroidImg.prototype, 'dtbOffset', {
get: function() {
if (this._m_dtbOffset !== undefined)
return this._m_dtbOffset;
if (this.headerVersion > 1) {
this._m_dtbOffset = (this.dtb.addr > 0 ? (this.dtb.addr - this.base) : 0);
}
return this._m_dtbOffset;
}
});
Object.defineProperty(AndroidImg.prototype, 'dtbImg', {
get: function() {
if (this._m_dtbImg !== undefined)
return this._m_dtbImg;
if ( ((this.headerVersion > 1) && (this.dtb.size > 0)) ) {
var _pos = this._io.pos;
this._io.seek((Math.floor(((((((this.pageSize + this.kernel.size) + this.ramdisk.size) + this.second.size) + this.recoveryDtbo.size) + this.pageSize) - 1) / this.pageSize) * this.pageSize));
this._m_dtbImg = this._io.readBytes(this.dtb.size);
this._io.seek(_pos);
}
return this._m_dtbImg;
}
});
Object.defineProperty(AndroidImg.prototype, 'ramdiskImg', {
get: function() {
if (this._m_ramdiskImg !== undefined)
return this._m_ramdiskImg;
if (this.ramdisk.size > 0) {
var _pos = this._io.pos;
this._io.seek((Math.floor((((this.pageSize + this.kernel.size) + this.pageSize) - 1) / this.pageSize) * this.pageSize));
this._m_ramdiskImg = this._io.readBytes(this.ramdisk.size);
this._io.seek(_pos);
}
return this._m_ramdiskImg;
}
});
Object.defineProperty(AndroidImg.prototype, 'recoveryDtboImg', {
get: function() {
if (this._m_recoveryDtboImg !== undefined)
return this._m_recoveryDtboImg;
if ( ((this.headerVersion > 0) && (this.recoveryDtbo.size > 0)) ) {
var _pos = this._io.pos;
this._io.seek(this.recoveryDtbo.offset);
this._m_recoveryDtboImg = this._io.readBytes(this.recoveryDtbo.size);
this._io.seek(_pos);
}
return this._m_recoveryDtboImg;
}
});
Object.defineProperty(AndroidImg.prototype, 'secondImg', {
get: function() {
if (this._m_secondImg !== undefined)
return this._m_secondImg;
if (this.second.size > 0) {
var _pos = this._io.pos;
this._io.seek((Math.floor(((((this.pageSize + this.kernel.size) + this.ramdisk.size) + this.pageSize) - 1) / this.pageSize) * this.pageSize));
this._m_secondImg = this._io.readBytes(this.second.size);
this._io.seek(_pos);
}
return this._m_secondImg;
}
});
/**
* base loading address
*/
Object.defineProperty(AndroidImg.prototype, 'base', {
get: function() {
if (this._m_base !== undefined)
return this._m_base;
this._m_base = (this.kernel.addr - 32768);
return this._m_base;
}
});
return AndroidImg;
})();
return AndroidImg;
}));