github.com/jlowellwofford/u-root@v1.0.0/pkg/bzimage/header.go (about)

     1  // Copyright 2015-2018 the u-root 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  // These are the semi-documented things that define a bzImage
     6  // Thanks to coreboot for documenting the basic layout.
     7  package bzimage
     8  
     9  const (
    10  	Ram      e820type = 1
    11  	Reserved          = 2
    12  	ACPI              = 3
    13  	NVS               = 4
    14  )
    15  
    16  const (
    17  	NotSet    boottype = 0
    18  	LoadLin            = 1
    19  	BootSect           = 2
    20  	SysLinux           = 3
    21  	EtherBoot          = 4
    22  	Kernel             = 5
    23  )
    24  
    25  const (
    26  	RamdiskStartMask = 0x07FF
    27  	Prompt           = 0x8000
    28  	Load             = 0x4000
    29  
    30  	CommandLineMagic = 0x7ff
    31  	CommandLineSize  = 256
    32  
    33  	DefaultInitrdAddrMax  = 0x37FFFFFF
    34  	DefaultBzimageAddrMax = 0x37FFFFFF
    35  
    36  	E820Max = 128
    37  	E820Map = 0x2d0
    38  	E820NR  = 0x1e8
    39  )
    40  
    41  // what's an EDD? No idea.
    42  /*
    43   * EDD stuff
    44   */
    45  
    46  const (
    47  	EDDMBRSigMax       = 16
    48  	EDDMaxNR           = 6 /* number of edd_info structs starting at EDDBUF  */
    49  	EDDDeviceParamSize = 74
    50  )
    51  
    52  const (
    53  	EDDExtFixedDiskAccess = 1 << iota
    54  	EDDExtDeviceLockingAndEjecting
    55  	EDDExtEnhancedDiskDriveSupport
    56  	EDDExt64BitExtensions
    57  )
    58  
    59  type EDDInfo struct {
    60  	Device                uint8
    61  	Version               uint8
    62  	InterfaceSupport      uint16
    63  	LegacyMaxCylinder     uint16
    64  	LegacyMaxHead         uint8
    65  	LegacySectorsPerTrace uint8
    66  	EDDDeviceParams       [EDDDeviceParamSize]uint8
    67  }
    68  
    69  type e820type uint32
    70  type boottype uint8
    71  type E820Entry struct {
    72  	Addr    uint64
    73  	Size    uint64
    74  	MemType e820type
    75  }
    76  
    77  // The header of Linux/i386 kernel
    78  type LinuxHeader struct {
    79  	MBRCode         [0xc0]uint8         `offset:"0x000"`
    80  	ExtRamdiskImage uint32              `offset:"0xc0"`
    81  	ExtRamdiskSize  uint32              `offset:"0xc4"`
    82  	ExtCmdlinePtr   uint32              `offset:"0xc8"`
    83  	_               [0x1f1 - 0xcc]uint8 `offset:"0xcc"`
    84  	SetupSects      uint8               `offset:"0x1f1"`
    85  	RootFlags       uint16              `offset:"0x1f2"`
    86  	Syssize         uint32              `offset:"0x1f4"` //(2.04+)
    87  	RamSize         uint16              `offset:"0x1f8"`
    88  	Vidmode         uint16              `offset:"0x1fa"`
    89  	RootDev         uint16              `offset:"0x1fc"`
    90  	Bootsectormagic uint16              `offset:"0x1fe"`
    91  	// 0.00+
    92  	Jump            uint16   `offset:"0x200"`
    93  	HeaderMagic     [4]uint8 `offset:"0x202"`
    94  	Protocolversion uint16   `offset:"0x206"`
    95  	RealModeSwitch  uint32   `offset:"0x208"`
    96  	StartSys        uint16   `offset:"0x20c"`
    97  	Kveraddr        uint16   `offset:"0x20e"`
    98  	TypeOfLoader    uint8    `offset:"0x210"`
    99  	Loadflags       uint8    `offset:"0x211"`
   100  	Setupmovesize   uint16   `offset:"0x212"`
   101  	Code32Start     uint32   `offset:"0x214"`
   102  	RamDiskImage    uint32   `offset:"0x218"`
   103  	RamDiskSize     uint32   `offset:"0x21c"`
   104  	BootSectKludge  [4]uint8 `offset:"0x220"`
   105  	// 2.01+
   106  	Heapendptr    uint16 `offset:"0x224"`
   107  	ExtLoaderVer  uint8  `offset:"0x226"`
   108  	ExtLoaderType uint8  `offset:"0x227"`
   109  	// 2.02+
   110  	Cmdlineptr uint32 `offset:"0x228"`
   111  	// 2.03+
   112  	InitrdAddrMax uint32 `offset:"0x22c"`
   113  	// 2.05+
   114  	Kernelalignment   uint32 `offset:"0x230"`
   115  	RelocatableKernel uint8  `offset:"0x234"`
   116  	MinAlignment      uint8  `offset:"0x235"` //(2.10+)
   117  	XLoadFlags        uint16 `offset:"0x236"`
   118  	// 2.06+
   119  	CmdLineSize uint32 `offset:"0x238"`
   120  	// 2.07+
   121  	HardwareSubArch     uint32 `offset:"0x23c"`
   122  	HardwareSubArchData uint64 `offset:"0x240"`
   123  	// 2.08+
   124  	PayloadOffset uint32 `offset:"0x248"`
   125  	PayloadSize   uint32 `offset:"0x24c"`
   126  	// 2.09+
   127  	SetupData uint64 `offset:"0x250"`
   128  	// 2.10+
   129  	PrefAddress    uint64 `offset:"0x258"`
   130  	InitSize       uint32 `offset:"0x260"`
   131  	HandoverOffset uint32 `offset:"0x264"`
   132  }
   133  
   134  // Paramters passed to 32-bit part of Linux
   135  type LinuxParams struct {
   136  	Origx           uint8  `offset:"0x00"`
   137  	Origy           uint8  `offset:"0x01"`
   138  	ExtMemK         uint16 `offset:"0x02"` //-- EXTMEMK sits here
   139  	OrigVideoPage   uint16 `offset:"0x04"`
   140  	OrigVideoMode   uint8  `offset:"0x06"`
   141  	OrigVideoCols   uint8  `offset:"0x07"`
   142  	_               uint16 `offset:"0x08"`
   143  	OrigVideoeGabx  uint16 `offset:"0x0a"`
   144  	_               uint16 `offset:"0x0c"`
   145  	OrigVideoLines  uint8  `offset:"0x0e"`
   146  	OrigVideoIsVGA  uint8  `offset:"0x0f"`
   147  	OrigVideoPoints uint16 `offset:"0x10"`
   148  
   149  	// VESA graphic mode -- linear frame buffer
   150  	Lfbwidth      uint16    `offset:"0x12"`
   151  	Lfbheight     uint16    `offset:"0x14"`
   152  	Lfbdepth      uint16    `offset:"0x16"`
   153  	Lfbbase       uint32    `offset:"0x18"`
   154  	Lfbsize       uint32    `offset:"0x1c"`
   155  	CLMagic       uint16    `offset:"0x20"` // DON'T USE
   156  	CLOffset      uint16    `offset:"0x22"` // DON'T USE
   157  	Lfblinelength uint16    `offset:"0x24"`
   158  	Redsize       uint8     `offset:"0x26"`
   159  	Redpos        uint8     `offset:"0x27"`
   160  	Greensize     uint8     `offset:"0x28"`
   161  	Greenpos      uint8     `offset:"0x29"`
   162  	Bluesize      uint8     `offset:"0x2a"`
   163  	Bluepos       uint8     `offset:"0x2b"`
   164  	Rsvdsize      uint8     `offset:"0x2c"`
   165  	Rsvdpos       uint8     `offset:"0x2d"`
   166  	Vesapmseg     uint16    `offset:"0x2e"`
   167  	Vesapmoff     uint16    `offset:"0x30"`
   168  	Pages         uint16    `offset:"0x32"`
   169  	_             [12]uint8 `offset:"0x34"` //-- 0x3f reserved for future expansion
   170  
   171  	//struct apmbiosinfo apmbiosinfo;
   172  	Apmbiosinfo [0x40]uint8 `offset:"0x40"`
   173  	//struct driveinfostruct driveinfo;
   174  	Driveinfo [0x20]uint8 `offset:"0x80"`
   175  	//struct sysdesctable sysdesctable;
   176  	Sysdesctable        [0x140]uint8 `offset:"0xa0"`
   177  	Altmemk             uint32       `offset:"0x1e0"`
   178  	_                   [4]uint8     `offset:"0x1e4"`
   179  	E820MapNr           uint8        `offset:"0x1e8"`
   180  	_                   [9]uint8     `offset:"0x1e9"`
   181  	MountRootReadonly   uint16       `offset:"0x1f2"`
   182  	_                   [4]uint8     `offset:"0x1f4"`
   183  	Ramdiskflags        uint16       `offset:"0x1f8"`
   184  	_                   [2]uint8     `offset:"0x1fa"`
   185  	OrigRootDev         uint16       `offset:"0x1fc"`
   186  	_                   [1]uint8     `offset:"0x1fe"`
   187  	Auxdeviceinfo       uint8        `offset:"0x1ff"`
   188  	_                   [2]uint8     `offset:"0x200"`
   189  	Paramblocksignature [4]uint8     `offset:"0x202"`
   190  	Paramblockversion   uint16       `offset:"0x206"`
   191  	_                   [8]uint8     `offset:"0x208"`
   192  	LoaderType          uint8        `offset:"0x210"`
   193  	Loaderflags         uint8        `offset:"0x211"`
   194  	_                   [2]uint8     `offset:"0x212"`
   195  	KernelStart         uint32       `offset:"0x214"`
   196  	Initrdstart         uint32       `offset:"0x218"`
   197  	Initrdsize          uint32       `offset:"0x21c"`
   198  	_                   [8]uint8     `offset:"0x220"`
   199  	CLPtr               uint32       `offset:"0x228"` // USE THIS.
   200  	InitrdAddrMax       uint32       `offset:"0x22c"`
   201  	/* 2.04+ */
   202  	KernelAlignment     uint32               `offset:"0x230"`
   203  	elocatableKernel    uint8                `offset:"0x234"`
   204  	MinAlignment        uint8                `offset:"0x235"`
   205  	XLoadFlags          uint16               `offset:"0x236"`
   206  	CmdLineSize         uint32               `offset:"0x238"`
   207  	HardwareSubarch     uint32               `offset:"0x23C"`
   208  	HardwareSubarchData uint64               `offset:"0x240"`
   209  	Payload_Ofset       uint32               `offset:"0x248"`
   210  	PayloadLength       uint32               `offset:"0x24C"`
   211  	SetupData           uint64               `offset:"0x250"`
   212  	PrefAddress         uint64               `offset:"0x258"`
   213  	InitSize            uint32               `offset:"0x260"`
   214  	HandoverOffset      uint32               `offset:"0x264"`
   215  	_                   [0x290 - 0x268]uint8 `offset:"0x268"`
   216  	EDDMBRSigBuffer     [EDDMBRSigMax]uint32 `offset:"0x290"`
   217  	// e820map is another cockup from the usual suspects.
   218  	// Go rounds the size to something reasonable. Oh well. No checking for you.
   219  	// So the next two offsets are bogus, sorry about that.
   220  	E820Map [E820Max]E820Entry `offset:"0x2d0"`
   221  	// we lie.
   222  	_      [48]uint8         `offset:"0xed0"` // `offset:"0xcd0"`
   223  	EDDBuf [EDDMaxNR]EDDInfo `offset:"0xf00"` // `offset:"0xd00"`
   224  
   225  }
   226  
   227  var (
   228  	LoaderType = map[boottype]string{
   229  		NotSet:    "Not set",
   230  		LoadLin:   "loadlin",
   231  		BootSect:  "bootsector",
   232  		SysLinux:  "syslinux",
   233  		EtherBoot: "etherboot",
   234  		Kernel:    "kernel (kexec)",
   235  	}
   236  	E820 = map[e820type]string{
   237  		Ram:      "Ram",
   238  		Reserved: "Reserved",
   239  		ACPI:     "ACPI",
   240  		NVS:      "NVS",
   241  	}
   242  	HeaderMagic = [4]uint8{'H', 'd', 'r', 'S'}
   243  )
   244  
   245  type BzImage struct {
   246  	Header       LinuxHeader
   247  	BootCode     []byte
   248  	Kernel       []byte
   249  	KernelBase   uintptr
   250  	KernelOffset uintptr
   251  	// todo: allow more than one?
   252  	InitRAMFS []byte
   253  }