2
* Copyright (C) 2004 Guillem Jover <guillem@debian.org>
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* 3. The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
#include <sys/types.h>
30
#include <sys/ioctl.h>
31
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD_kernel__)
32
#include <sys/param.h>
33
#if defined(__FreeBSD_kernel__) && __FreeBSD_kernel_version >= 500000
36
#include <sys/disklabel.h>
37
#ifndef DIOCGMEDIASIZE
38
#define DIOCGMEDIASIZE _IOR('d', 129, off_t)
40
#elif defined(__linux__)
42
#define BLKGETSIZE _IO(0x12,96)
49
static const int sector_size = 512;
52
get_block_device_size(int fd)
57
#if defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__NetBSD__)
59
struct disklabel disklabel;
62
if (!ioctl(fd, DIOCGMEDIASIZE, &device_size))
65
if (!ioctl(fd, DIOCGDINFO, &disklabel)) {
66
size = ((int64_t)disklabel.d_secperunit) * sector_size;
70
#elif defined(__linux__)
72
long int device_sectors;
74
if (!ioctl(fd, BLKGETSIZE, &device_sectors)) {
75
size = ((int64_t)device_sectors) * sector_size;
80
/* XXX: Implement a generic seek binary test for targets currently
83
#error "Do not know how to get block device size"
86
/* Regular files or devices under the Hurd. */
87
if (fstat64(fd, &st) == 0) {