#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$NetBSD: header.c,v 1.12 2025/12/17 15:56:06 nia Exp $");
#endif
#include <sys/types.h>
#if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H)
#include <sys/endian.h>
#endif
#include <err.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "map.h"
#include "gpt.h"
#include "gpt_private.h"
static int cmd_header(gpt_t, int, char *[]);
static const char *headerhelp[] = {
"",
};
const struct gpt_cmd c_header = {
"header",
cmd_header,
headerhelp, __arraycount(headerhelp),
GPT_READONLY,
};
#define usage() gpt_usage(NULL, &c_header)
static int
header(gpt_t gpt)
{
unsigned int revision;
map_t map;
struct gpt_hdr *hdr;
char buf[128];
gpt_show_num("Media Size", (uintmax_t)gpt->mediasz);
printf("Sector Size: %u\n", gpt->secsz);
gpt_show_num("Number of Sectors",
(uintmax_t)(gpt->mediasz / gpt->secsz));
printf("\nHeader Information:\n");
map = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
if (map == NULL) {
printf("- GPT Header not found\n");
return -1;
}
hdr = map->map_data;
revision = le32toh(hdr->hdr_revision);
printf("- GPT Header Revision: %u.%u\n", revision >> 16,
revision & 0xffff);
gpt_show_num("- First Data Sector", le64toh(hdr->hdr_lba_start));
gpt_show_num("- Last Data Sector", le64toh(hdr->hdr_lba_end));
gpt_uuid_snprintf(buf, sizeof(buf), "%d", hdr->hdr_guid);
printf("- Media GUID: %s\n", buf);
printf("- Number of GPT Entries: %u\n", le32toh(hdr->hdr_entries));
return 0;
}
static int
cmd_header(gpt_t gpt, int argc, char *argv[] __unused)
{
if (argc != optind)
return usage();
return header(gpt);
}