aboutsummaryrefslogtreecommitdiffstats
path: root/src/pmbr.c
blob: a9bbfb862a125744a746b09e14afa2c2e2a1c37b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "project.h"


void
mbr_entry_show (MBR_entry * e)
{
  printf ("      flags  %02x start chs %5d/%3d/%2d start sector %10d\n",
          e->bootable,
          MBR_CYLINDER (e->chs_start.cs),
          e->chs_start.head, MBR_SECTOR (e->chs_start.cs), e->start);

  printf ("      system %02x end   chs %5d/%3d/%2d end   sector %10d\n",
          e->system,
          MBR_CYLINDER (e->chs_end.cs),
          e->chs_end.head, MBR_SECTOR (e->chs_end.cs), e->size + e->start);
}

void
mbr_show (MBR * m)
{
  int i;

  printf ("  PMBR:\n");

  for (i = 0; i < 4; ++i)
    {
      if (m->entry[i].system)
        {
          printf ("    Entry %d:\n", i);
          mbr_entry_show (&m->entry[i]);
        }
    }
  if (memcmp (m->signature, MBR_SIGNATURE, sizeof (m->signature)))
    {
      printf ("    Signature INVALID\n");
    }
  else
    {
      printf ("    Signature valid\n");
    }
}

MBR
mbr_new (uint64_t lbas)
{
  uint64_t cyls = lbas;
  MBR ret = { 0 };

  cyls = cyls / (63 * 255);
  if (cyls > 1023)
    cyls = 1023;

  ret.entry[0].bootable = 0;
  ret.entry[0].chs_start.cs = MBR_CS (0, 1);
  ret.entry[0].system = MBR_PARTITION_TYPE_EFI;
  ret.entry[0].chs_end.head = 254;
  ret.entry[0].chs_end.cs = MBR_CS (cyls, 63);
  ret.entry[0].start = 1;
  ret.entry[0].size = lbas - 1;

  memcpy (ret.signature, MBR_SIGNATURE, sizeof (ret.signature));

  return ret;
}