Skip to content

Commit dac9b9f

Browse files
committed
first commit
0 parents  commit dac9b9f

File tree

10 files changed

+684
-0
lines changed

10 files changed

+684
-0
lines changed

.github/workflows/build.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Build
2+
3+
on:
4+
push
5+
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
with:
13+
fetch-depth: 0
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: 1.16
19+
20+
- name: Build
21+
run: bash ./buildAllPlatforms.sh
22+
23+
- name: Upload artifacts
24+
uses: actions/upload-artifact@v2
25+
with:
26+
name: bin
27+
path: bin/*
28+
29+
- name: Release
30+
if: startsWith(github.ref, 'refs/tags')
31+
uses: softprops/action-gh-release@v1
32+
with:
33+
files:
34+
bin/*

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2021, hzyitc
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

amlCRC.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package AmlImg
2+
3+
import "hash/crc32"
4+
5+
// NOTE: Diffenent from the standard CRC32
6+
func AmlCRC(crc uint32, p []byte) uint32 {
7+
table := crc32.MakeTable(0xedb88320)
8+
return ^crc32.Update(^crc, table, p)
9+
}

buildAllPlatforms.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
PROGRAM="AmlImg"
4+
CLI="./cli"
5+
OUTPUT="bin/"
6+
LDFLAGS="-s -w"
7+
8+
ver="$(git describe --tags --match "v*" --dirty="" 2>/dev/null || git log -1 --pretty=format:"v0.0.0-%h" 2>/dev/null || echo "v0.0.0")"
9+
[ -n "$(git status --porcelain |& grep -Ev '^\?\?')" ] && ver="$ver-$(date +"%Y%M%d-%H%m%S")"
10+
LDFLAGS="$LDFLAGS -X main.version=$ver"
11+
12+
mkdir -p "${OUTPUT}"
13+
rm -f "${OUTPUT}/${PROGRAM}_"*
14+
15+
platforms=(
16+
linux/386
17+
linux/amd64
18+
linux/arm
19+
linux/arm64
20+
linux/mips/softfloat
21+
linux/mips64
22+
linux/mips64le
23+
linux/mipsle/softfloat
24+
windows/386
25+
windows/amd64
26+
windows/arm
27+
)
28+
# platforms=($(go tool dist list))
29+
30+
for i in "${platforms[@]}"; do
31+
os="$(echo "$i" | awk -F/ '{print $1}')"
32+
arch="$(echo "$i" | awk -F/ '{print $2}')"
33+
mips="$(echo "$i" | awk -F/ '{print $3}')"
34+
35+
[ "$os" == "windows" ] && ext="exe"
36+
37+
filename="${OUTPUT}/${PROGRAM}_${ver}_${os}_${arch}${ext:+.$ext}"
38+
echo "build $filename for $i"
39+
CGO_ENABLED=0 GOOS="${os}" GOARCH="${arch}" GOMIPS="${mips}" \
40+
go build -trimpath -ldflags "$LDFLAGS" -o "${filename}" ${CLI}
41+
done

cli/main.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"os"
9+
"strings"
10+
11+
"github.com/hzyitc/AmlImg"
12+
)
13+
14+
var version = "v0.0.0"
15+
16+
func usage() {
17+
print(os.Args[0] + " (" + version + ")\n")
18+
print("Usage:\n")
19+
print(" " + os.Args[0] + " unpack <img path> <extract dir path>\n")
20+
print(" " + os.Args[0] + " pack <img path> <dir path>\n")
21+
}
22+
23+
func main() {
24+
if len(os.Args) != 4 {
25+
usage()
26+
return
27+
}
28+
29+
switch os.Args[1] {
30+
case "unpack":
31+
os.MkdirAll(os.Args[3], 755)
32+
33+
err := unpack(os.Args[2], os.Args[3])
34+
if err != nil {
35+
println(err.Error())
36+
return
37+
}
38+
39+
case "pack":
40+
err := pack(os.Args[2], os.Args[3])
41+
if err != nil {
42+
println(err.Error())
43+
return
44+
}
45+
46+
}
47+
}
48+
49+
func unpack(filePath, extractPath string) error {
50+
img, err := AmlImg.NewReader(filePath, true)
51+
if err != nil {
52+
return errors.New("NewReader error: " + err.Error())
53+
}
54+
defer img.Close()
55+
56+
cmdfile, err := os.Create(extractPath + "/commands.txt")
57+
if err != nil {
58+
return errors.New("Create error: " + err.Error())
59+
}
60+
defer cmdfile.Close()
61+
62+
for i := 0; i < int(img.Header.ItemCount); i++ {
63+
item := img.Items[i]
64+
65+
filename := fmt.Sprintf("%d.%s.%s", item.Id, item.Name, item.Type)
66+
if item.ImgType == AmlImg.ImgType_Sparse {
67+
filename += ".sparse"
68+
}
69+
70+
println("Extracting ", extractPath+"/"+filename)
71+
72+
imtType := "unknown"
73+
if item.ImgType == AmlImg.ImgType_Normal {
74+
imtType = "normal"
75+
} else if item.ImgType == AmlImg.ImgType_Sparse {
76+
imtType = "sparse"
77+
}
78+
fmt.Fprintf(cmdfile, "%s:%s:%s:%s\n", item.Type, item.Name, imtType, filename)
79+
80+
file, err := os.Create(extractPath + "/" + filename)
81+
if err != nil {
82+
return errors.New("Create error:" + err.Error())
83+
}
84+
85+
err = img.Seek(uint32(i), 0)
86+
if err != nil {
87+
file.Close()
88+
return errors.New("Seek error:" + err.Error())
89+
}
90+
91+
_, err = io.Copy(file, img)
92+
if err != nil {
93+
file.Close()
94+
return errors.New("Copy error:" + err.Error())
95+
}
96+
97+
file.Close()
98+
}
99+
100+
return nil
101+
}
102+
103+
func pack(filePath, dirPath string) error {
104+
img, err := AmlImg.NewWriter()
105+
if err != nil {
106+
return errors.New("NewWriter error: " + err.Error())
107+
}
108+
109+
cmdfile, err := os.Open(dirPath + "/commands.txt")
110+
if err != nil {
111+
return errors.New("Open error: " + err.Error())
112+
}
113+
defer cmdfile.Close()
114+
115+
scanner := bufio.NewScanner(cmdfile)
116+
scanner.Split(bufio.ScanWords)
117+
118+
for scanner.Scan() {
119+
txt := scanner.Text()
120+
if txt == "" {
121+
continue
122+
} else if strings.HasPrefix(txt, "#") {
123+
continue
124+
}
125+
126+
c := strings.SplitN(txt, ":", 4)
127+
Type := c[0]
128+
Name := c[1]
129+
filename := c[3]
130+
131+
imgType := AmlImg.ImgType_Normal
132+
switch c[2] {
133+
case "normal":
134+
imgType = AmlImg.ImgType_Normal
135+
case "sparse":
136+
imgType = AmlImg.ImgType_Sparse
137+
default:
138+
return errors.New("unknown imgType: " + c[2])
139+
}
140+
141+
img.Add(Type, Name, imgType, func(w io.Writer) error {
142+
println("Packing ", filename)
143+
144+
file, err := os.Open(dirPath + "/" + filename)
145+
if err != nil {
146+
return errors.New("Open error: " + err.Error())
147+
}
148+
149+
_, err = io.Copy(w, file)
150+
return err
151+
})
152+
}
153+
154+
err = scanner.Err()
155+
if err != nil {
156+
return errors.New("scanner error: " + err.Error())
157+
}
158+
159+
err = img.Write(filePath, 2)
160+
if err != nil {
161+
return errors.New("Write error: " + err.Error())
162+
}
163+
164+
return nil
165+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/hzyitc/AmlImg
2+
3+
go 1.16

header.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package AmlImg
2+
3+
import (
4+
"encoding/binary"
5+
"io"
6+
)
7+
8+
const (
9+
Magic = uint32(0x27B51956)
10+
)
11+
12+
type Header struct {
13+
CRC uint32
14+
Version uint32
15+
Magic uint32
16+
Size uint64
17+
AlignSize uint32
18+
ItemCount uint32
19+
Reserved [36]byte
20+
}
21+
22+
func Header_Unpack(reader io.Reader) (*Header, error) {
23+
header := Header{}
24+
err := binary.Read(reader, binary.LittleEndian, &header)
25+
return &header, err
26+
}
27+
28+
func (header *Header) Pack(writer io.Writer) error {
29+
return binary.Write(writer, binary.LittleEndian, header)
30+
}

0 commit comments

Comments
 (0)