forked from anestisb/android-simg2img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimg_samsungify.d
97 lines (87 loc) · 2.67 KB
/
simg_samsungify.d
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
/**
* Copyright (C) 2018 Vladimir Panteleev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Description:
*
* Tweak a sparse-image file to conform to Samsung's proprietary
* format modifications.
*
* This allows creating sparse-image files directly flashable e.g. on
* SM-N910H with Odin/Heimdall.
*/
module simg_samsungify;
import std.exception;
import std.mmfile;
import std.stdio;
struct SparseHeader
{
uint magic = 0xed26ff3a;
ushort major_version = 1;
ushort minor_version = 0;
ushort file_hdr_sz;
ushort chunk_hdr_sz;
uint blk_sz;
uint total_blks;
uint total_chunks;
uint image_checksum;
}
struct ChunkHeader
{
ubyte[2] chunk_type;
ushort reserved1;
uint chunk_sz;
uint total_sz;
}
immutable uint[1] headerExtra = [0];
immutable uint[1] chunkExtra = [0x00007fcb];
void main(string[] args)
{
enforce(args.length == 3, "Usage: simg_samsungify IN-FILE-NAME OUT-FILE-NAME");
string inFileName = args[1];
string outFileName = args[2];
auto m = new MmFile(inFileName);
enforce(m.length >= SparseHeader.sizeof, "Not enough bytes for header");
auto p = cast(ubyte*)m[].ptr;
auto pHeader = cast(SparseHeader*)p;
enforce(pHeader.file_hdr_sz == SparseHeader.sizeof,
"Wrong file header size (already samsung-ified?)");
enforce(pHeader.chunk_hdr_sz == ChunkHeader.sizeof,
"Wrong chunk header size (already samsung-ified?)");
auto newHeader = *pHeader;
newHeader.file_hdr_sz += headerExtra.sizeof;
newHeader.chunk_hdr_sz += chunkExtra.sizeof;
auto o = File(outFileName, "wb");
o.rawWrite((&newHeader)[0..1]);
o.rawWrite(headerExtra[]);
auto end = p + m.length;
p += pHeader.file_hdr_sz;
while (p < end)
{
enforce(p + pHeader.chunk_hdr_sz <= end,
"Not enough bytes for chunk header");
auto pChunkHeader = cast(ChunkHeader*)p;
//writeln(*pChunkHeader);
enforce(p + pChunkHeader.total_sz <= end,
"Not enough bytes for chunk");
auto dataLength = pChunkHeader.total_sz - pHeader.chunk_hdr_sz;
auto newChunkHeader = *pChunkHeader;
newChunkHeader.total_sz += chunkExtra.sizeof;
o.rawWrite((&newChunkHeader)[0..1]);
o.rawWrite(chunkExtra);
p += pHeader.chunk_hdr_sz;
o.rawWrite(p[0..dataLength]);
p += dataLength;
}
}