aboutsummaryrefslogtreecommitdiffstats
path: root/src/disk.c
blob: b6bf85444fa13e0703d544436f2ed8fb6507f768 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "project.h"
#include <sys/ioctl.h>
#include <linux/fs.h>


DISK *
disk_open (char *fn)
{
  DISK *ret = calloc (1, sizeof (DISK));
  off_t o;

  ret->fd = open (fn, O_RDWR);

  if (ret->fd < 0)
    {
      free (ret);
      return NULL;
    }


  ret->name = strdup (fn);

  ret->lbas = lseek (ret->fd, (off_t) 0, SEEK_END);



  ret->lbas = ret->lbas / (uint64_t) SECTOR_SIZE;

  fprintf (stderr, "Opened %s with size of %lld lbas\n", ret->name,
           (long long) ret->lbas);

  return ret;
}

void
disk_read (DISK * d, void *buf, uint64_t lba, int lbas)
{
  lba *= SECTOR_SIZE;

  if (lseek (d->fd, lba, SEEK_SET) != lba)
    {
      perror ("lseek");
      abort ();
    }

  lbas *= SECTOR_SIZE;
  if (read (d->fd, buf, lbas) != lbas)
    {
      perror ("read");
      //    abort ();
    }
}

void
disk_write (DISK * d, void *buf, uint64_t lba, int lbas)
{
  lba *= SECTOR_SIZE;

  if (lseek (d->fd, lba, SEEK_SET) != lba)
    {
      perror ("lseek");
      abort ();
    }

  lbas *= SECTOR_SIZE;
  if (write (d->fd, buf, lbas) != lbas)
    {
      perror ("write");
      abort ();
    }
}

uint64_t
disk_lbas (DISK * d)
{
  return d->lbas;
}

disk_reread_kernel_table (DISK * d)
{
  if (ioctl (d->fd, BLKRRPART, NULL))
    {
      perror ("Telling kernel to re-read partition table failed:");
    }
  else
    {
      fprintf (stderr, "Kernel has re-read partition table\n");
    }
}