From a8bb10ce9c79ad70b3488facdfa287464df91f74 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Sat, 18 Feb 2023 23:01:06 -0800 Subject: [PATCH] bootdeploy: copy implementation from cmd/mkinitfs --- cmd/mkinitfs/main.go | 10 ++- internal/bootdeploy/bootdeploy.go | 119 +++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/cmd/mkinitfs/main.go b/cmd/mkinitfs/main.go index 5371783..da6ac93 100644 --- a/cmd/mkinitfs/main.go +++ b/cmd/mkinitfs/main.go @@ -67,7 +67,15 @@ func main() { if err != nil { log.Fatal("Unable to create temporary work directory:", err) } - defer os.RemoveAll(workDir) + defer func() { + e := os.RemoveAll(workDir) + if e != nil && err == nil { + err = e + } + if err != nil { + log.Fatal(err) + } + }() log.Print("Generating for kernel version: ", kernVer) log.Print("Output directory: ", *outDir) diff --git a/internal/bootdeploy/bootdeploy.go b/internal/bootdeploy/bootdeploy.go index 1206a10..e7bf025 100644 --- a/internal/bootdeploy/bootdeploy.go +++ b/internal/bootdeploy/bootdeploy.go @@ -1,6 +1,16 @@ package bootdeploy -import () +import ( + "errors" + "io" + "log" + "os" + "os/exec" + "path/filepath" + "strings" + + "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc" +) type BootDeploy struct { inDir string @@ -25,5 +35,112 @@ func New(inDir, outDir, ubootBoardname string) *BootDeploy { func (b *BootDeploy) Run() error { + if err := copyUbootFiles(b.inDir, b.ubootBoardname); errors.Is(err, os.ErrNotExist) { + log.Println("u-boot files copying skipped: ", err) + } else { + if err != nil { + log.Fatal("copyUbootFiles: ", err) + } + } + + return bootDeploy(b.inDir, b.outDir) +} + +func bootDeploy(workDir string, outDir string) error { + // boot-deploy expects the kernel to be in the same dir as initramfs. + // Assume that the kernel is in the output dir... + kernels, _ := filepath.Glob(filepath.Join(outDir, "vmlinuz*")) + if len(kernels) == 0 { + return errors.New("Unable to find any kernels at " + filepath.Join(outDir, "vmlinuz*")) + } + + // Pick a kernel that does not have suffixes added by boot-deploy + var kernFile string + for _, f := range kernels { + if strings.HasSuffix(f, "-dtb") || strings.HasSuffix(f, "-mtk") { + continue + } + kernFile = f + break + } + + kernFd, err := os.Open(kernFile) + if err != nil { + return err + } + defer kernFd.Close() + + kernFileCopy, err := os.Create(filepath.Join(workDir, "vmlinuz")) + if err != nil { + return err + } + + if _, err = io.Copy(kernFileCopy, kernFd); err != nil { + return err + } + kernFileCopy.Close() + + // boot-deploy -i initramfs -k vmlinuz-postmarketos-rockchip -d /tmp/cpio -o /tmp/foo initramfs-extra + cmd := exec.Command("boot-deploy", + "-i", "initramfs", + "-k", "vmlinuz", + "-d", workDir, + "-o", outDir, + "initramfs-extra") + if !misc.Exists(cmd.Path) { + return errors.New("boot-deploy command not found") + } + + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Print("'boot-deploy' command failed") + return err + } + + return nil +} + +func copy(srcFile, dstFile string) error { + out, err := os.Create(dstFile) + if err != nil { + return err + } + + defer out.Close() + + in, err := os.Open(srcFile) + if err != nil { + return err + } + defer in.Close() + + _, err = io.Copy(out, in) + if err != nil { + return err + } + + return nil +} + +func copyUbootFiles(path, ubootBoardname string) error { + if ubootBoardname == "" { + return nil + } + + srcDir := filepath.Join("/usr/share/u-boot", ubootBoardname) + entries, err := os.ReadDir(srcDir) + if err != nil { + return err + } + for _, entry := range entries { + sourcePath := filepath.Join(srcDir, entry.Name()) + destPath := filepath.Join(path, entry.Name()) + + if err := copy(sourcePath, destPath); err != nil { + return err + } + } + return nil }