Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

system.raw合并完成,修改后重新打碎成system_*.img #1

@ch0729

Description

@ch0729

这是我根据你的packsparseimg源码写的

虽然可以打碎,但因为我合并后要修改,修改后打碎,刷手机,就开机定屏

`package main

import (
"encoding/xml"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)

type Data struct {
Program []Program xml:"program"
}

type Program struct {
SECTOR_SIZE_IN_BYTES int64 xml:"SECTOR_SIZE_IN_BYTES,attr"
File_sector_offset int64 xml:"file_sector_offset,attr"
Filename string xml:"filename,attr"
Label string xml:"label,attr"
Num_partition_sectors int64 xml:"num_partition_sectors,attr"
Physical_partition_number int64 xml:"physical_partition_number,attr"
Size_in_KB string xml:"size_in_KB,attr"
Sparse string xml:"sparse,attr"
Start_byte_hex string xml:"start_byte_hex,attr"
Start_sector int64 xml:"start_sector,attr"
}

type Image struct {
ImageType string
XMLName string
OutPath string
romDir string
data Data
startSector int64
sectorPos int64
}

var (
merge = "merge"
split = "split"
ext4 = ".ext4"
mergeStr = "合并"
splitStr = "分割"

action string

)

func main() {
var image Image
flag.StringVar(&image.XMLName, "x", "rawprogram_unsparse.xml", "XML文件名")
flag.StringVar(&image.OutPath, "o", "./", "输出目录")
flag.StringVar(&image.romDir, "d", "./", "固件目录")
flag.StringVar(&image.ImageType, "t", "system", "Image类型: system|cache|userdata|apps")
flag.StringVar(&action, "a", "merge", "选择功能: merge|split")
flag.Parse()

switch action {
case merge:
	image.ShowInfo(mergeStr)
	image.Merge()
case split:
	image.ShowInfo(splitStr)
	image.Split()
default:
	fmt.Println("执行出错: -a 参数应为(merge|split)")
}

}

func (i *Image) ShowInfo(s string) {
fmt.Printf("------------------------------ 执行信息 ------------------------------"+
"\n 固件目录 : %s\n Image类型: %s\n XML文件名: %s"+
"\n 输出目录 : %s\n 本次执行 : %s\n"+
"----------------------------------------------------------------------\n",
i.romDir, i.ImageType, i.XMLName, i.OutPath, s)
}

func (i *Image) Split() {
i.ParseXML()

f, e := os.Open(i.romDir + "/" + i.ImageType + ext4)
if e != nil {
	panic(e)
}
defer f.Close()

for _, p := range i.data.Program {
	if p.Label == i.ImageType {

		i.SplitID(p)

		i.sectorPos = (p.Start_sector - i.startSector) * p.SECTOR_SIZE_IN_BYTES
		f.Seek(i.sectorPos, os.SEEK_SET)

		fileSize := p.Num_partition_sectors * p.SECTOR_SIZE_IN_BYTES

		read := make([]byte, fileSize)
		f.Read(read)

		file, e := os.Create(i.OutPath + "/" + p.Filename)
		if e != nil {
			panic(e)
		}

		file.Write(read)
		file.Close()
		fmt.Printf("%s	从%d位开始, 分割大小为%d的%s\n", i.ImageType+ext4, i.sectorPos, fileSize, p.Filename)
	}
}
fmt.Printf("分割%s完成, %s_*.img输出目录: %s\n", i.ImageType+ext4, i.ImageType, i.OutPath)

}

func (i *Image) SplitID(p Program) {
id := strings.Split(strings.Split(p.Filename, "_")[1], ".")[0]
if id == "1" {
i.startSector = p.Start_sector

}

}

func (i *Image) Merge() {
i.ParseXML()

fileName := i.OutPath + "/" + i.ImageType + ext4
f, e := os.Create(fileName)
if e != nil {
	panic(e)
}
defer f.Close()

for _, p := range i.data.Program {
	if p.Label == i.ImageType {

		i.SplitID(p)

		i.sectorPos = (p.Start_sector - i.startSector) * p.SECTOR_SIZE_IN_BYTES
		f.Seek(i.sectorPos, os.SEEK_SET)

		file, e := os.Open(i.romDir + "/" + p.Filename)
		if e != nil {
			panic(e)
		}

		fileInfo, _ := file.Stat()

		io.Copy(f, file)
		file.Close()

		fmt.Printf("%s	大小%d	从%d位开始合并\n", p.Filename, fileInfo.Size(), i.sectorPos)
	}
}

fmt.Printf("合并%s完成, %s输出目录: %s\n", i.ImageType, i.ImageType+ext4, i.OutPath)

}

func (i *Image) ParseXML() {
f, e := ioutil.ReadFile(i.romDir + "/" + i.XMLName)
if e != nil {
panic(e.Error())
}

e = xml.Unmarshal(f, &i.data)
if e != nil {
	panic(e)
}
if len(i.data.Program) == 0 {
	panic("XML数据为空")
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions