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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
* zgetdump - Tool for copying and converting System z dumps
*
* Helper functions
*
* Copyright IBM Corp. 2001, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#ifndef ZG_H
#define ZG_H
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "lib/util_base.h"
#include "lib/zt_common.h"
#define U64_MAX ((u64) -1)
#define U32_MAX ((u32) -1)
#define U16_MAX ((u16) -1)
#define U8_MAX ((u8) -1)
/*
* IEC definitions
*/
#define KIB (1024)
#define MIB (1024 * 1024)
#define GIB (1024 * 1024 * 1024)
#define TO_MIB(x) ((x + (MIB / 2)) / MIB)
#define TO_KIB(x) ((x + (KIB / 2)) / KIB)
/*
* Memory functions
*/
extern void *zg_alloc(unsigned int size);
extern void *zg_realloc(void *ptr, unsigned int size);
extern void zg_free(void *ptr);
extern char *zg_strdup(const char *str);
/*
* At exit functions
*/
typedef void (*zg_atexit_fn_t)(void);
extern void zg_atexit(zg_atexit_fn_t fn);
extern void __noreturn zg_exit(int rc);
/*
* Temporary device node functions
*/
extern char *zg_devnode_create(dev_t dev);
/*
* Progress bar functions
*/
extern void zg_progress_init(const char *msg, u64 mem_size);
extern void zg_progress(u64 addr);
/*
* Error and print functions
*/
#define ERR(x...) \
do { \
fprintf(stderr, "%s: ", "zgetdump"); \
fprintf(stderr, x); \
fprintf(stderr, "\n"); \
} while (0)
#define ERR_EXIT(x...) \
do { \
ERR(x); \
zg_exit(1); \
} while (0)
#define ABORT(x...) \
do { \
ERR("Internal Error: " x); \
abort(); \
} while (0)
#define ERR_EXIT_ERRNO(x...) \
do { \
fflush(stdout); \
fprintf(stderr, "%s: ", "zgetdump"); \
fprintf(stderr, x); \
fprintf(stderr, " (%s)", strerror(errno)); \
fprintf(stderr, "\n"); \
zg_exit(1); \
} while (0)
#define STDERR(x...) \
do { \
fprintf(stderr, x); \
fflush(stderr); \
} while (0)
#define STDERR_PR(x...) \
do { \
fprintf(stderr, "\r%s: ", "zgetdump"); \
fprintf(stderr, x); \
} while (0)
#define STDOUT(x...) \
do { \
fprintf(stdout, x); \
fflush(stdout); \
} while (0)
/*
* Misc
*/
#define PAGE_SIZE 4096UL
#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE)
#define ARRAY_ELEMENT_CNT(x) (sizeof(x) / sizeof(x[0]))
#define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
static inline u32 zg_csum_partial(const void *buf, int len, u32 sum)
{
register unsigned long reg2 asm("2") = (unsigned long) buf;
register unsigned long reg3 asm("3") = (unsigned long) len;
asm volatile(
"0: cksm %0,%1\n" /* do checksum on longs */
" jo 0b\n"
: "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory");
return sum;
}
/*
* Pointer atrithmetic
*/
#define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
#define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
#define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
/*
* File functions
*/
struct zg_fh {
const char *path;
int fh;
struct stat sb;
};
enum zg_type {
ZG_TYPE_DASD,
ZG_TYPE_DASD_PART,
ZG_TYPE_FILE,
ZG_TYPE_TAPE,
ZG_TYPE_UNKNOWN,
};
enum zg_check {
ZG_CHECK,
ZG_CHECK_ERR,
ZG_CHECK_NONE,
};
extern const char *zg_path(struct zg_fh *zg_fh);
extern const struct stat *zg_stat(struct zg_fh *zg_fh);
extern struct zg_fh *zg_open(const char *path, int flags, enum zg_check check);
extern void zg_close(struct zg_fh *zg_fh);
extern ssize_t zg_read(struct zg_fh *zg_fh, void *buf, size_t cnt,
enum zg_check check);
extern ssize_t zg_gets(struct zg_fh *zg_fh, void *buf, size_t cnt,
enum zg_check check);
extern u64 zg_size(struct zg_fh *zg_fh);
extern off_t zg_tell(struct zg_fh *zg_fh, enum zg_check check);
extern off_t zg_seek(struct zg_fh *zg_fh, off_t off, enum zg_check check);
extern off_t zg_seek_end(struct zg_fh *zg_fh, off_t off, enum zg_check check);
extern off_t zg_seek_cur(struct zg_fh *zg_fh, off_t off, enum zg_check check);
extern int zg_ioctl(struct zg_fh *zg_fh, int rq, void *data, const char *op,
enum zg_check check);
extern enum zg_type zg_type(struct zg_fh *zg_fh);
/*
* zgetdump actions
*/
enum zg_action {
ZG_ACTION_STDOUT,
ZG_ACTION_DUMP_INFO,
ZG_ACTION_DEVICE_INFO,
ZG_ACTION_MOUNT,
ZG_ACTION_UMOUNT,
};
#endif /* ZG_H */
|