github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/fit/calc_offset.go (about) 1 // Copyright 2017-2021 the LinuxBoot Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package fit 6 7 import "github.com/linuxboot/fiano/pkg/intel/metadata/fit/consts" 8 9 // CalculatePhysAddrFromOffset calculates the physical address (address to a 10 // region mapped from the SPI chip) using an offset withtin an image, relatively 11 // to BasePhysAddr. 12 // 13 // Examples: 14 // CalculatePhysAddrFromOffset(0x01, 0x2000) == 0xffffe001 15 // CalculatePhysAddrFromOffset(0x40, 0x2000) == 0xffffe040 16 func CalculatePhysAddrFromOffset(offset uint64, imageSize uint64) uint64 { 17 startAddr := consts.BasePhysAddr - imageSize 18 return startAddr + offset 19 } 20 21 // CalculateOffsetFromPhysAddr calculates the offset within an image 22 // of the physical address (address to a region mapped from 23 // the SPI chip). 24 // 25 // Examples: 26 // CalculateOffsetFromPhysAddr(0xffffffff, 0x1000) == 0xfff 27 // CalculateOffsetFromPhysAddr(0xffffffc0, 0x1000) == 0xfc0 28 func CalculateOffsetFromPhysAddr(physAddr uint64, imageSize uint64) uint64 { 29 startAddr := consts.BasePhysAddr - imageSize 30 return physAddr - startAddr 31 } 32 33 // CalculateTailOffsetFromPhysAddr calculates the offset (towards down, relatively 34 // to BasePhysAddr) of the physical address (address to a region mapped from 35 // the SPI chip). 36 // 37 // Examples: 38 // CalculateTailOffsetFromPhysAddr(0xffffffff) == 0x01 39 // CalculateTailOffsetFromPhysAddr(0xffffffc0) == 0x40 40 func CalculateTailOffsetFromPhysAddr(physAddr uint64) uint64 { 41 return consts.BasePhysAddr - physAddr 42 }