Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hzyitc committed Jun 12, 2022
0 parents commit dac9b9f
Show file tree
Hide file tree
Showing 10 changed files with 684 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Build

on:
push

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Build
run: bash ./buildAllPlatforms.sh

- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: bin
path: bin/*

- name: Release
if: startsWith(github.ref, 'refs/tags')
uses: softprops/action-gh-release@v1
with:
files:
bin/*
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2021, hzyitc
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9 changes: 9 additions & 0 deletions amlCRC.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package AmlImg

import "hash/crc32"

// NOTE: Diffenent from the standard CRC32
func AmlCRC(crc uint32, p []byte) uint32 {
table := crc32.MakeTable(0xedb88320)
return ^crc32.Update(^crc, table, p)
}
41 changes: 41 additions & 0 deletions buildAllPlatforms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

PROGRAM="AmlImg"
CLI="./cli"
OUTPUT="bin/"
LDFLAGS="-s -w"

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")"
[ -n "$(git status --porcelain |& grep -Ev '^\?\?')" ] && ver="$ver-$(date +"%Y%M%d-%H%m%S")"
LDFLAGS="$LDFLAGS -X main.version=$ver"

mkdir -p "${OUTPUT}"
rm -f "${OUTPUT}/${PROGRAM}_"*

platforms=(
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips/softfloat
linux/mips64
linux/mips64le
linux/mipsle/softfloat
windows/386
windows/amd64
windows/arm
)
# platforms=($(go tool dist list))

for i in "${platforms[@]}"; do
os="$(echo "$i" | awk -F/ '{print $1}')"
arch="$(echo "$i" | awk -F/ '{print $2}')"
mips="$(echo "$i" | awk -F/ '{print $3}')"

[ "$os" == "windows" ] && ext="exe"

filename="${OUTPUT}/${PROGRAM}_${ver}_${os}_${arch}${ext:+.$ext}"
echo "build $filename for $i"
CGO_ENABLED=0 GOOS="${os}" GOARCH="${arch}" GOMIPS="${mips}" \
go build -trimpath -ldflags "$LDFLAGS" -o "${filename}" ${CLI}
done
165 changes: 165 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package main

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/hzyitc/AmlImg"
)

var version = "v0.0.0"

func usage() {
print(os.Args[0] + " (" + version + ")\n")
print("Usage:\n")
print(" " + os.Args[0] + " unpack <img path> <extract dir path>\n")
print(" " + os.Args[0] + " pack <img path> <dir path>\n")
}

func main() {
if len(os.Args) != 4 {
usage()
return
}

switch os.Args[1] {
case "unpack":
os.MkdirAll(os.Args[3], 755)

err := unpack(os.Args[2], os.Args[3])
if err != nil {
println(err.Error())
return
}

case "pack":
err := pack(os.Args[2], os.Args[3])
if err != nil {
println(err.Error())
return
}

}
}

func unpack(filePath, extractPath string) error {
img, err := AmlImg.NewReader(filePath, true)
if err != nil {
return errors.New("NewReader error: " + err.Error())
}
defer img.Close()

cmdfile, err := os.Create(extractPath + "/commands.txt")
if err != nil {
return errors.New("Create error: " + err.Error())
}
defer cmdfile.Close()

for i := 0; i < int(img.Header.ItemCount); i++ {
item := img.Items[i]

filename := fmt.Sprintf("%d.%s.%s", item.Id, item.Name, item.Type)
if item.ImgType == AmlImg.ImgType_Sparse {
filename += ".sparse"
}

println("Extracting ", extractPath+"/"+filename)

imtType := "unknown"
if item.ImgType == AmlImg.ImgType_Normal {
imtType = "normal"
} else if item.ImgType == AmlImg.ImgType_Sparse {
imtType = "sparse"
}
fmt.Fprintf(cmdfile, "%s:%s:%s:%s\n", item.Type, item.Name, imtType, filename)

file, err := os.Create(extractPath + "/" + filename)
if err != nil {
return errors.New("Create error:" + err.Error())
}

err = img.Seek(uint32(i), 0)
if err != nil {
file.Close()
return errors.New("Seek error:" + err.Error())
}

_, err = io.Copy(file, img)
if err != nil {
file.Close()
return errors.New("Copy error:" + err.Error())
}

file.Close()
}

return nil
}

func pack(filePath, dirPath string) error {
img, err := AmlImg.NewWriter()
if err != nil {
return errors.New("NewWriter error: " + err.Error())
}

cmdfile, err := os.Open(dirPath + "/commands.txt")
if err != nil {
return errors.New("Open error: " + err.Error())
}
defer cmdfile.Close()

scanner := bufio.NewScanner(cmdfile)
scanner.Split(bufio.ScanWords)

for scanner.Scan() {
txt := scanner.Text()
if txt == "" {
continue
} else if strings.HasPrefix(txt, "#") {
continue
}

c := strings.SplitN(txt, ":", 4)
Type := c[0]
Name := c[1]
filename := c[3]

imgType := AmlImg.ImgType_Normal
switch c[2] {
case "normal":
imgType = AmlImg.ImgType_Normal
case "sparse":
imgType = AmlImg.ImgType_Sparse
default:
return errors.New("unknown imgType: " + c[2])
}

img.Add(Type, Name, imgType, func(w io.Writer) error {
println("Packing ", filename)

file, err := os.Open(dirPath + "/" + filename)
if err != nil {
return errors.New("Open error: " + err.Error())
}

_, err = io.Copy(w, file)
return err
})
}

err = scanner.Err()
if err != nil {
return errors.New("scanner error: " + err.Error())
}

err = img.Write(filePath, 2)
if err != nil {
return errors.New("Write error: " + err.Error())
}

return nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/hzyitc/AmlImg

go 1.16
30 changes: 30 additions & 0 deletions header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package AmlImg

import (
"encoding/binary"
"io"
)

const (
Magic = uint32(0x27B51956)
)

type Header struct {
CRC uint32
Version uint32
Magic uint32
Size uint64
AlignSize uint32
ItemCount uint32
Reserved [36]byte
}

func Header_Unpack(reader io.Reader) (*Header, error) {
header := Header{}
err := binary.Read(reader, binary.LittleEndian, &header)
return &header, err
}

func (header *Header) Pack(writer io.Writer) error {
return binary.Write(writer, binary.LittleEndian, header)
}
Loading

0 comments on commit dac9b9f

Please sign in to comment.