syslinux/libinstaller/syslxint.h
<<
>>
Prefs
   1/* ----------------------------------------------------------------------- *
   2 *
   3 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
   4 *   Copyright 2009-2011 Intel Corporation; author: H. Peter Anvin
   5 *
   6 *   This program is free software; you can redistribute it and/or modify
   7 *   it under the terms of the GNU General Public License as published by
   8 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
   9 *   Boston MA 02111-1307, USA; either version 2 of the License, or
  10 *   (at your option) any later version; incorporated herein by reference.
  11 *
  12 * ----------------------------------------------------------------------- */
  13
  14#ifndef SYSLXINT_H
  15#define SYSLXINT_H
  16
  17#include "syslinux.h"
  18
  19#if defined(__386__) || defined(__i386__) || defined(__x86_64__)
  20# define X86_MEM 1              /* Littleendian and unaligned safe */
  21#else
  22# define X86_MEM 0
  23#endif
  24
  25/*
  26 * Access functions for littleendian numbers, possibly misaligned.
  27 */
  28static inline uint8_t get_8(const uint8_t * p)
  29{
  30    return *p;
  31}
  32
  33static inline uint16_t get_16(const uint16_t * p)
  34{
  35#if X86_MEM
  36    /* Littleendian and unaligned-capable */
  37    return *p;
  38#else
  39    const uint8_t *pp = (const uint8_t *)p;
  40    return pp[0] + ((uint16_t)pp[1] << 8);
  41#endif
  42}
  43
  44static inline uint32_t get_32(const uint32_t * p)
  45{
  46#if X86_MEM
  47    /* Littleendian and unaligned-capable */
  48    return *p;
  49#else
  50    const uint16_t *pp = (const uint16_t *)p;
  51    return get_16(pp[0]) + (uint32_t)get_16(pp[1]);
  52#endif
  53}
  54
  55static inline uint64_t get_64(const uint64_t * p)
  56{
  57#if X86_MEM
  58    /* Littleendian and unaligned-capable */
  59    return *p;
  60#else
  61    const uint32_t *pp = (const uint32_t *)p;
  62    return get_32(pp[0]) + (uint64_t)get_32(pp[1]);
  63#endif
  64}
  65
  66static inline void set_8(uint8_t *p, uint8_t v)
  67{
  68    *p = v;
  69}
  70
  71static inline void set_16(uint16_t *p, uint16_t v)
  72{
  73#if X86_MEM
  74    /* Littleendian and unaligned-capable */
  75    *p = v;
  76#else
  77    uint8_t *pp = (uint8_t *) p;
  78    pp[0] = (v & 0xff);
  79    pp[1] = ((v >> 8) & 0xff);
  80#endif
  81}
  82
  83static inline void set_32(uint32_t *p, uint32_t v)
  84{
  85#if X86_MEM
  86    /* Littleendian and unaligned-capable */
  87    *p = v;
  88#else
  89    uint8_t *pp = (uint8_t *) p;
  90    pp[0] = (v & 0xff);
  91    pp[1] = ((v >> 8) & 0xff);
  92    pp[2] = ((v >> 16) & 0xff);
  93    pp[3] = ((v >> 24) & 0xff);
  94#endif
  95}
  96
  97static inline void set_64(uint64_t *p, uint64_t v)
  98{
  99#if X86_MEM
 100    /* Littleendian and unaligned-capable */
 101    *p = v;
 102#else
 103    uint32_t *pp = (uint32_t *) p;
 104    set_32(pp[0], v);
 105    set_32(pp[1], v >> 32);
 106#endif
 107}
 108
 109/*
 110 * Special handling for the MS-DOS derivative: syslinux_ldlinux
 111 * is a "far" object...
 112 */
 113#ifdef __MSDOS__
 114
 115static inline __attribute__ ((const))
 116uint16_t ds(void)
 117{
 118    uint16_t v;
 119    asm("movw %%ds,%0":"=rm"(v));
 120    return v;
 121}
 122
 123static inline void *set_fs(const void *p)
 124{
 125    uint16_t seg;
 126
 127    seg = ds() + ((size_t) p >> 4);
 128    asm volatile ("movw %0,%%fs"::"rm" (seg));
 129    return (void *)((size_t) p & 0xf);
 130}
 131
 132uint8_t get_8_sl(const uint8_t * p);
 133uint16_t get_16_sl(const uint16_t * p);
 134uint32_t get_32_sl(const uint32_t * p);
 135uint64_t get_64_sl(const uint64_t * p);
 136void set_8_sl(uint8_t * p, uint8_t v);
 137void set_16_sl(uint16_t * p, uint16_t v);
 138void set_32_sl(uint32_t * p, uint32_t v);
 139void set_64_sl(uint64_t * p, uint64_t v);
 140void memcpy_to_sl(void *dst, const void *src, size_t len);
 141void memcpy_from_sl(void *dst, const void *src, size_t len);
 142
 143#else
 144
 145/* Sane system ... */
 146#define get_8_sl(x)             get_8(x)
 147#define get_16_sl(x)            get_16(x)
 148#define get_32_sl(x)            get_32(x)
 149#define get_64_sl(x)            get_64(x)
 150#define set_8_sl(x,y)           set_8(x,y)
 151#define set_16_sl(x,y)          set_16(x,y)
 152#define set_32_sl(x,y)          set_32(x,y)
 153#define set_64_sl(x,y)          set_64(x,y)
 154#define memcpy_to_sl(d,s,l)     memcpy(d,s,l)
 155#define memcpy_from_sl(d,s,l)   memcpy(d,s,l)
 156
 157#endif
 158
 159#define LDLINUX_MAGIC   0x3eb202fe
 160#define BS_MAGIC_VER    (0x1b << 9)
 161
 162/* Patch area for disk-based installers */
 163struct patch_area {
 164    uint32_t magic;             /* LDLINUX_MAGIC */
 165    uint32_t instance;          /* Per-version value */
 166    uint16_t data_sectors;
 167    uint16_t adv_sectors;
 168    uint32_t dwords;
 169    uint32_t checksum;
 170    uint16_t maxtransfer;
 171    uint16_t epaoffset;         /* Pointer to the extended patch area */
 172};
 173
 174struct ext_patch_area {
 175    uint16_t advptroffset;      /* ADV pointers */
 176    uint16_t diroffset;         /* Current directory field */
 177    uint16_t dirlen;            /* Length of current directory field */
 178    uint16_t subvoloffset;      /* Subvolume field */
 179    uint16_t subvollen;         /* Length of subvolume field */
 180    uint16_t secptroffset;      /* Sector extent pointers */
 181    uint16_t secptrcnt;         /* Number of sector extent pointers */
 182
 183    uint16_t sect1ptr0;         /* Boot sector offset of sector 1 ptr LSW */
 184    uint16_t sect1ptr1;         /* Boot sector offset of sector 1 ptr MSW */
 185    uint16_t raidpatch;         /* Boot sector RAID mode patch pointer */
 186};
 187
 188/* Sector extent */
 189struct syslinux_extent {
 190    uint64_t lba;
 191    uint16_t len;
 192} __attribute__((packed));
 193
 194/* FAT bootsector format, also used by other disk-based derivatives */
 195struct boot_sector {
 196    uint8_t bsJump[3];
 197    char bsOemName[8];
 198    uint16_t bsBytesPerSec;
 199    uint8_t bsSecPerClust;
 200    uint16_t bsResSectors;
 201    uint8_t bsFATs;
 202    uint16_t bsRootDirEnts;
 203    uint16_t bsSectors;
 204    uint8_t bsMedia;
 205    uint16_t bsFATsecs;
 206    uint16_t bsSecPerTrack;
 207    uint16_t bsHeads;
 208    uint32_t bsHiddenSecs;
 209    uint32_t bsHugeSectors;
 210
 211    union {
 212        struct {
 213            uint8_t DriveNumber;
 214            uint8_t Reserved1;
 215            uint8_t BootSignature;
 216            uint32_t VolumeID;
 217            char VolumeLabel[11];
 218            char FileSysType[8];
 219            uint8_t Code[442];
 220        } __attribute__ ((packed)) bs16;
 221        struct {
 222            uint32_t FATSz32;
 223            uint16_t ExtFlags;
 224            uint16_t FSVer;
 225            uint32_t RootClus;
 226            uint16_t FSInfo;
 227            uint16_t BkBootSec;
 228            uint8_t Reserved0[12];
 229            uint8_t DriveNumber;
 230            uint8_t Reserved1;
 231            uint8_t BootSignature;
 232            uint32_t VolumeID;
 233            char VolumeLabel[11];
 234            char FileSysType[8];
 235            uint8_t Code[414];
 236        } __attribute__ ((packed)) bs32;
 237    } __attribute__ ((packed));
 238
 239    uint32_t bsMagic;
 240    uint16_t bsForwardPtr;
 241    uint16_t bsSignature;
 242} __attribute__ ((packed));
 243
 244#define bsHead      bsJump
 245#define bsHeadLen   offsetof(struct boot_sector, bsBytesPerSec)
 246#define bsCode      bs32.Code   /* The common safe choice */
 247#define bsCodeLen   (offsetof(struct boot_sector, bsSignature) - \
 248                     offsetof(struct boot_sector, bsCode))
 249
 250#endif /* SYSLXINT_H */
 251
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.