1cf200d32Sopenharmony_ci/* bsd.h -- BSD disklabel data structure definitions, types, and functions */ 2cf200d32Sopenharmony_ci 3cf200d32Sopenharmony_ci/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed 4cf200d32Sopenharmony_ci under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ 5cf200d32Sopenharmony_ci 6cf200d32Sopenharmony_ci#ifndef __BSD_STRUCTS 7cf200d32Sopenharmony_ci#define __BSD_STRUCTS 8cf200d32Sopenharmony_ci 9cf200d32Sopenharmony_ci#include <stdint.h> 10cf200d32Sopenharmony_ci#include <sys/types.h> 11cf200d32Sopenharmony_ci#include "gptpart.h" 12cf200d32Sopenharmony_ci#include "diskio.h" 13cf200d32Sopenharmony_ci 14cf200d32Sopenharmony_ci#define BSD_SIGNATURE UINT32_C(0x82564557) /* BSD disklabel signature ("magic") */ 15cf200d32Sopenharmony_ci 16cf200d32Sopenharmony_ci// BSD disklabels can start at offsets of 64 or the sector size -- at least, 17cf200d32Sopenharmony_ci// I *THINK* that's what's going on. I've seen them at 64 or 512 on disks 18cf200d32Sopenharmony_ci// with 512-byte blocks and at 2048 on disks with 2048-byte blocks. The 19cf200d32Sopenharmony_ci// LABEL_OFFSET2 value will be replaced by the block size in the 20cf200d32Sopenharmony_ci// ReadBSDData() function.... 21cf200d32Sopenharmony_ci#define LABEL_OFFSET1 64 22cf200d32Sopenharmony_ci#define LABEL_OFFSET2 512 23cf200d32Sopenharmony_ci#define NUM_OFFSETS 2 24cf200d32Sopenharmony_ci 25cf200d32Sopenharmony_ci// FreeBSD documents a maximum # of partitions of 8, but I saw 16 on a NetBSD 26cf200d32Sopenharmony_ci// disk. I'm quadrupling that for further safety. Note that BSDReadData() 27cf200d32Sopenharmony_ci// uses a 4096-byte I/O buffer. In combination with LABEL_OFFSET3 and the 28cf200d32Sopenharmony_ci// additional 148-byte offset to the actual partition data, that gives a 29cf200d32Sopenharmony_ci// theoretical maximum of 118.75 partitions that the program can handle before 30cf200d32Sopenharmony_ci// memory errors will occur. 31cf200d32Sopenharmony_ci#define MAX_BSD_PARTS 64 32cf200d32Sopenharmony_ci 33cf200d32Sopenharmony_ci/**************************************** 34cf200d32Sopenharmony_ci * * 35cf200d32Sopenharmony_ci * BSDData class and related structures * 36cf200d32Sopenharmony_ci * * 37cf200d32Sopenharmony_ci ****************************************/ 38cf200d32Sopenharmony_ci 39cf200d32Sopenharmony_ci// Possible states of the MBR 40cf200d32Sopenharmony_cienum BSDValidity {unknown, bsd_invalid, bsd}; 41cf200d32Sopenharmony_ci 42cf200d32Sopenharmony_ci// Data for a single BSD partition record 43cf200d32Sopenharmony_ci// Create entries for all fields, although we only use lengthLBA, firstLBA, 44cf200d32Sopenharmony_ci// and fsType, to simplify loading the data from disk.... 45cf200d32Sopenharmony_cistruct BSDRecord { // the partition table 46cf200d32Sopenharmony_ci uint32_t lengthLBA; // number of sectors in partition 47cf200d32Sopenharmony_ci uint32_t firstLBA; // starting sector 48cf200d32Sopenharmony_ci uint32_t fragSize; // filesystem basic fragment size 49cf200d32Sopenharmony_ci uint8_t fsType; // filesystem type, see below 50cf200d32Sopenharmony_ci uint8_t frag; // filesystem fragments per block 51cf200d32Sopenharmony_ci uint16_t pcpg; // filesystem cylinders per group 52cf200d32Sopenharmony_ci}; 53cf200d32Sopenharmony_ci 54cf200d32Sopenharmony_ci// Full data in tweaked BSD format 55cf200d32Sopenharmony_ci// For some reason this has to be packed or MS Visual C++'s debugger complains 56cf200d32Sopenharmony_ci// about memory errors whenever a BSDData variable is destroyed. 57cf200d32Sopenharmony_ci#pragma pack (8) 58cf200d32Sopenharmony_ciclass BSDData { 59cf200d32Sopenharmony_ci protected: 60cf200d32Sopenharmony_ci // We only need a few items from the main BSD disklabel data structure.... 61cf200d32Sopenharmony_ci uint32_t signature; // the magic number 62cf200d32Sopenharmony_ci uint32_t sectorSize; // # of bytes per sector 63cf200d32Sopenharmony_ci uint32_t signature2; // the magic number (again) 64cf200d32Sopenharmony_ci uint16_t numParts; // number of partitions in table 65cf200d32Sopenharmony_ci struct BSDRecord* partitions; // partition array 66cf200d32Sopenharmony_ci 67cf200d32Sopenharmony_ci // Above are basic BSD disklabel data; now add more stuff.... 68cf200d32Sopenharmony_ci uint64_t labelFirstLBA; // first sector of BSD disklabel (partition or disk) 69cf200d32Sopenharmony_ci uint64_t labelLastLBA; // final sector of BSD disklabel 70cf200d32Sopenharmony_ci uint64_t labelStart; // BSD disklabel start point in bytes from labelFirstLBA 71cf200d32Sopenharmony_ci BSDValidity state; 72cf200d32Sopenharmony_ci public: 73cf200d32Sopenharmony_ci BSDData(void); 74cf200d32Sopenharmony_ci ~BSDData(void); 75cf200d32Sopenharmony_ci int ReadBSDData(const std::string & deviceFilename, uint64_t startSector, uint64_t endSector); 76cf200d32Sopenharmony_ci int ReadBSDData(DiskIO *myDisk, uint64_t startSector, uint64_t endSector); 77cf200d32Sopenharmony_ci void ReverseMetaBytes(void); 78cf200d32Sopenharmony_ci void DisplayBSDData(void); 79cf200d32Sopenharmony_ci int ShowState(void); // returns 1 if BSD disklabel detected 80cf200d32Sopenharmony_ci int IsDisklabel(void); 81cf200d32Sopenharmony_ci 82cf200d32Sopenharmony_ci // Functions to extract data on specific partitions.... 83cf200d32Sopenharmony_ci uint8_t GetType(int i); 84cf200d32Sopenharmony_ci uint64_t GetFirstSector(int i); 85cf200d32Sopenharmony_ci uint64_t GetLength(int i); 86cf200d32Sopenharmony_ci int GetNumParts(void); 87cf200d32Sopenharmony_ci GPTPart AsGPT(int i); // Return BSD part. as GPT part. 88cf200d32Sopenharmony_ci}; // struct MBRData 89cf200d32Sopenharmony_ci#pragma pack () 90cf200d32Sopenharmony_ci 91cf200d32Sopenharmony_ci#endif 92