/*
 * s390-tools/zipl/include/disk.h
 *   Functions to handle disk layout specific operations.
 *
 * Copyright (C) 2001-2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
 *
 * Author(s): Carsten Otte <cotte@de.ibm.com>
 *            Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
 */

#ifndef DISK_H
#define DISK_H

#include "zipl.h"

#include <stdint.h>
#include <sys/types.h>
#include <linux/hdreg.h>


/* Type for representing disk block numbers */
typedef uint64_t blocknum_t;

/* Pointer to block on a disk with cyl/head/sec layout */
struct disk_blockptr_chs {
	int cyl;
	int head;
	int sec;
	int size;
	int blockct;
};

/* Pointer to a block on a disk with linear layout */
struct disk_blockptr_linear {
	blocknum_t block;
	int size;
	int blockct;
};

/* Pointer to a block on disk */
typedef union {
	struct disk_blockptr_chs chs;
	struct disk_blockptr_linear linear;
} disk_blockptr_t;

/* Disk type identifier */
typedef enum {
	disk_type_scsi,
	disk_type_fba,
	disk_type_diag,
	disk_type_eckd_classic,
	disk_type_eckd_compatible,
	disk_type_unknown
} disk_type_t;

/* Disk information type */
struct disk_info {
	disk_type_t type;
	dev_t device;
	dev_t partition;
	int devno;
	int partnum;
	int phy_block_size;
	int fs_block_size;
	uint64_t phy_blocks;
	struct hd_geometry geo;
	char* name;
};


int disk_get_info(const char* device, struct disk_info** info);
int disk_get_info_from_file(const char* filename, struct disk_info** info);
void disk_free_info(struct disk_info* info);
char* disk_get_type_name(disk_type_t type);
int disk_cyl_from_blocknum(blocknum_t blocknum, struct disk_info* info);
int disk_head_from_blocknum(blocknum_t blocknum, struct disk_info* info);
int disk_sec_from_blocknum(blocknum_t blocknum, struct disk_info* info);
void disk_blockptr_from_blocknum(disk_blockptr_t* ptr, blocknum_t blocknum,
				 struct disk_info* info);
int disk_write_block_aligned(int fd, const void* data, size_t bytecount,
	  		     disk_blockptr_t* block, struct disk_info* info);
blocknum_t disk_write_block_buffer(int fd, const void* buffer,
				   size_t bytecount,
				   disk_blockptr_t** blocklist,
				   struct disk_info* info);
void disk_print_devt(dev_t d);
void disk_print_info(struct disk_info* info);
int disk_is_zero_block(disk_blockptr_t* block, struct disk_info* info);
blocknum_t disk_compact_blocklist(disk_blockptr_t* list, blocknum_t count,
				  struct disk_info* info);
blocknum_t disk_get_blocklist_from_file(const char* filename,
					disk_blockptr_t** blocklist,
					struct disk_info* pinfo);

#endif /* not DISK_H */
