blob: 2dcebabbaaf854bc54b38b0dfa8280ab8f9ef5fd [file] [log] [blame] [edit]
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cosboot
import (
"bytes"
"compress/gzip"
"debug/elf"
"errors"
"io"
)
func elfFromBZImageWithOffset(img []byte) ([]byte, int, error) {
gzipMagic := []byte{0x1f, 0x8b, 0x08}
for searchOffset := 0; searchOffset < len(img); {
i := bytes.Index(img[searchOffset:], gzipMagic)
if i == -1 {
break
}
i += searchOffset
searchOffset = i + 1
// Found a potential gzip header, attempt decompression.
gzReader, err := gzip.NewReader(bytes.NewReader(img[i:]))
if err != nil {
continue
}
gzReader.Multistream(false)
decompressed, err := io.ReadAll(gzReader)
gzReader.Close()
if err != nil {
continue
}
// Verify if the decompressed content is a valid ELF file.
if f, err := elf.NewFile(bytes.NewReader(decompressed)); err == nil {
f.Close()
return decompressed, i, nil
}
}
return nil, 0, errors.New("could not find a compressed ELF payload in the bzImage")
}
// elfFromBZImage converts a given bzImage into an ELF. Adapted from
// extract-vmlinux in the kernel tree.
func elfFromBZImage(img []byte) ([]byte, error) {
data, _, err := elfFromBZImageWithOffset(img)
if err != nil {
return nil, err
}
return data, nil
}
// packBZImage packs a given ELF into a given bzImage.
func packBZImage(bzImg, elfImg []byte) ([]byte, error) {
_, offset, err := elfFromBZImageWithOffset(bzImg)
if err != nil {
return nil, err
}
var compressed bytes.Buffer
// Go's gzip implementation appears to output compressed data that is ~10%
// larger than what the gzip utility would output, so use BestCompression
// here to try and compensate as best we can (it doesn't seem to help a ton)
gzWriter, err := gzip.NewWriterLevel(&compressed, gzip.BestCompression)
if err != nil {
return nil, err
}
defer gzWriter.Close()
if _, err := gzWriter.Write(elfImg); err != nil {
return nil, err
}
if err := gzWriter.Close(); err != nil {
return nil, err
}
return append(bzImg[:offset], compressed.Bytes()...), nil
}