Skip to content

Commit 3f79076

Browse files
committed
First commit.
0 parents  commit 3f79076

File tree

10 files changed

+583
-0
lines changed

10 files changed

+583
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

LICENSE

+143
Large diffs are not rendered by default.

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Rockchip resource image (RSCE) Pack/Unpack Tool written in Go
2+
3+
`Author:` [@evsio0n](https://evsio0n.com)
4+
5+
----
6+
7+
8+
## Usage
9+
10+
`-u`: Unpack the RSCE file to the specified directory.
11+
12+
e.g. `./rsce_tool -u ./boot-second`
13+
14+
`-p`: Pack the file to RSCE file.
15+
16+
e.g. `./rsce_tool -p ./rk-kernel.dtb -p ./logo.bmp -p ./logo-kernel.bmp`
17+

go.mod

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module main
2+
3+
go 1.14
4+
5+
replace rsce => ./rsce
6+
7+
require (
8+
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
9+
github.com/evsio0n/log v0.0.0-20220309083450-856743806fca
10+
github.com/urfave/cli v1.22.5
11+
rsce v0.0.0-00010101000000-000000000000
12+
)

go.sum

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
4+
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
5+
github.com/evsio0n/log v0.0.0-20220309083450-856743806fca h1:ramJKiYbGj8mCx18pcnE6Ye67Av9T3xTUca6d+CQRAU=
6+
github.com/evsio0n/log v0.0.0-20220309083450-856743806fca/go.mod h1:YcVGix+QIHnfRILtBRAedOfLzxKKNrO1wN6Lfyj3dvw=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
9+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
10+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
11+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
12+
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
13+
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
*
3+
* @Author evsio0n
4+
* @Date 2022/3/27 下午3:53
5+
* @Email <[email protected]>
6+
*
7+
*/
8+
9+
package main
10+
11+
import (
12+
"github.com/evsio0n/log"
13+
"github.com/urfave/cli"
14+
"os"
15+
"rsce"
16+
"sort"
17+
)
18+
19+
const (
20+
name = "rsce-go"
21+
author = "evsio0n <[email protected]>"
22+
version = "0.0.1"
23+
description = "rsce-go is a tool for unpack or pack RSCE, aka rock chip resource image. \r\n" +
24+
"\t using -u to unpack, using -p to pack.\r\n" +
25+
"\t -u [filepath] unpack rockchip rsce image\r\n" +
26+
"\t -p [filepath] [filepath] [filepath] pack all resource to rockchip rsce image\r\n"
27+
)
28+
29+
func main() {
30+
cmd := &cli.App{
31+
Name: name,
32+
Author: author,
33+
Version: version,
34+
Description: description,
35+
Flags: []cli.Flag{
36+
cli.StringFlag{
37+
Name: "unpack,u",
38+
Usage: "unpack rsce image",
39+
},
40+
cli.StringSliceFlag{
41+
Name: "pack,p",
42+
Usage: "pack rsce image",
43+
},
44+
},
45+
Action: Handle,
46+
}
47+
sort.Sort(cli.FlagsByName(cmd.Flags))
48+
err := cmd.Run(os.Args)
49+
if err != nil {
50+
log.Error(err.Error())
51+
}
52+
}
53+
func Handle(c *cli.Context) {
54+
55+
if c.String("unpack") != "" {
56+
rsce.UnPackRSCE(c.String("unpack"))
57+
}
58+
59+
if len(c.StringSlice("pack")) > 0 {
60+
rsce.GenerateRSCE(c.StringSlice("pack"), "./boot-second")
61+
}
62+
}

rsce/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module RSCE
2+
3+
go 1.14
4+
5+
require github.com/evsio0n/log v0.0.0-20220309083450-856743806fca // indirect

rsce/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/evsio0n/log v0.0.0-20220309083450-856743806fca h1:ramJKiYbGj8mCx18pcnE6Ye67Av9T3xTUca6d+CQRAU=
2+
github.com/evsio0n/log v0.0.0-20220309083450-856743806fca/go.mod h1:YcVGix+QIHnfRILtBRAedOfLzxKKNrO1wN6Lfyj3dvw=

rsce/stuct.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
*
3+
* @Author evsio0n
4+
* @Date 2022/3/27 下午3:53
5+
* @Email <[email protected]>
6+
*
7+
*/
8+
9+
// Package rsce aka Rockchip resources image.
10+
//It's a binary file which contains device tree blob and additional resources
11+
//(like vendor splash screen) and appears as boot.img-second on unpacking.
12+
package rsce
13+
14+
import (
15+
"encoding/binary"
16+
)
17+
18+
const (
19+
HeaderMagic = "RSCE"
20+
EntryMagic = "ENTR"
21+
)
22+
23+
type Header struct {
24+
Magic [4]byte //"RSCE"
25+
RSCEver uint16 //0x0000
26+
RSCEfileTblVer uint16 //0x0000
27+
HdrBlkSize byte //0x01
28+
FileTblBlkOffset byte //0x01
29+
FileTblRecBlkSize byte //0x01
30+
Unknown byte //0x00
31+
FileCount uint32
32+
//.... All along calculated as 512 bytes.
33+
//and all in little endian.
34+
//Reserved [496]byte
35+
}
36+
37+
type FileEntry struct {
38+
Magic [4]byte //"ENTR"
39+
FileName [256]byte
40+
FileBlkOffset uint32
41+
FileSize uint32
42+
43+
//.... All along calculated as 512 bytes.
44+
//Reserved [244]byte
45+
}
46+
type fileEntry struct {
47+
FileName string
48+
FileBlkOffset uint32
49+
FileSize uint32
50+
}
51+
52+
func removeZeroByte(src []byte) []byte {
53+
var strBuf []byte
54+
for _, v := range src {
55+
if v != 0 {
56+
strBuf = append(strBuf, v)
57+
}
58+
}
59+
return strBuf
60+
}
61+
62+
func (h *Header) ToBytes() [512]byte {
63+
var headerBuffer [512]byte
64+
//copy header to buffer
65+
//string to byte
66+
HeaderMagicByte := make([]byte, 4)
67+
HeaderMagicByte = []byte(HeaderMagic)
68+
69+
copy(headerBuffer[:4], HeaderMagicByte)
70+
copy(headerBuffer[4:6], toLittleEndianBytes(uint(h.RSCEver), 2, 16))
71+
copy(headerBuffer[6:8], toLittleEndianBytes(uint(h.RSCEfileTblVer), 2, 16))
72+
copy(headerBuffer[8:9], []byte{h.HdrBlkSize})
73+
copy(headerBuffer[9:10], []byte{h.FileTblBlkOffset})
74+
copy(headerBuffer[10:11], []byte{h.FileTblRecBlkSize})
75+
copy(headerBuffer[11:12], []byte{h.Unknown})
76+
copy(headerBuffer[12:16], toLittleEndianBytes(uint(h.FileCount), 4, 16))
77+
return headerBuffer
78+
}
79+
80+
func (f *fileEntry) ToBytes() [512]byte {
81+
var fileEntryBuffer [512]byte
82+
//copy file entry to buffer
83+
//string to byte
84+
EntryMagicByte := make([]byte, 4)
85+
EntryMagicByte = []byte(EntryMagic)
86+
//string extend to 256 bytes
87+
fileName := f.FileName
88+
//null safe if file name is more than 256 bytes
89+
if len(fileName) > 256 {
90+
fileName = fileName[:256]
91+
}
92+
var fileNameBuffer [256]byte
93+
copy(fileNameBuffer[:len(fileName)], fileName)
94+
copy(fileEntryBuffer[:4], EntryMagicByte)
95+
copy(fileEntryBuffer[4:260], fileNameBuffer[:])
96+
copy(fileEntryBuffer[260:264], toLittleEndianBytes(uint(f.FileBlkOffset), 4, 32))
97+
copy(fileEntryBuffer[264:268], toLittleEndianBytes(uint(f.FileSize), 4, 32))
98+
return fileEntryBuffer
99+
}
100+
101+
func toLittleEndianBytes(num uint, byteLength int, uintByte int) []byte {
102+
littleEndianBytes := make([]byte, byteLength)
103+
//check uint type
104+
switch uintByte {
105+
case 16:
106+
binary.LittleEndian.PutUint16(littleEndianBytes[:], uint16(num))
107+
case 32:
108+
binary.LittleEndian.PutUint32(littleEndianBytes[:], uint32(num))
109+
//default. wes should not be here
110+
default:
111+
binary.LittleEndian.PutUint16(littleEndianBytes[:], uint16(num))
112+
}
113+
return littleEndianBytes
114+
}

0 commit comments

Comments
 (0)