1235537Sgber/*-
2235537Sgber * Copyright (c) 2010-2012 Semihalf.
3235537Sgber * All rights reserved.
4235537Sgber *
5235537Sgber * Redistribution and use in source and binary forms, with or without
6235537Sgber * modification, are permitted provided that the following conditions
7235537Sgber * are met:
8235537Sgber * 1. Redistributions of source code must retain the above copyright
9235537Sgber *    notice, this list of conditions and the following disclaimer.
10235537Sgber * 2. Redistributions in binary form must reproduce the above copyright
11235537Sgber *    notice, this list of conditions and the following disclaimer in the
12235537Sgber *    documentation and/or other materials provided with the distribution.
13235537Sgber *
14235537Sgber * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15235537Sgber * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16235537Sgber * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17235537Sgber * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18235537Sgber * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19235537Sgber * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20235537Sgber * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21235537Sgber * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22235537Sgber * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23235537Sgber * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24235537Sgber * SUCH DAMAGE.
25235537Sgber */
26235537Sgber
27235537Sgber#include <sys/cdefs.h>
28235537Sgber__FBSDID("$FreeBSD: releng/11.0/usr.sbin/nandtool/nand_read.c 235537 2012-05-17 10:11:18Z gber $");
29235537Sgber
30235537Sgber#include <stdio.h>
31235537Sgber#include <stdlib.h>
32235537Sgber#include <unistd.h>
33235537Sgber#include <fcntl.h>
34235537Sgber#include <libgeom.h>
35235537Sgber#include <sys/disk.h>
36235537Sgber#include <dev/nand/nand_dev.h>
37235537Sgber#include "nandtool.h"
38235537Sgber
39235537Sgberint nand_read(struct cmd_param *params)
40235537Sgber{
41235537Sgber	struct chip_param_io chip_params;
42235537Sgber	int fd = -1, out_fd = -1, done = 0, ret = 0;
43235537Sgber	char *dev, *out;
44235537Sgber	int pos, count, mult, block_size;
45235537Sgber	uint8_t *buf = NULL;
46235537Sgber
47235537Sgber	if (!(dev = param_get_string(params, "dev"))) {
48235537Sgber		fprintf(stderr, "You must specify 'dev' parameter\n");
49235537Sgber		return (1);
50235537Sgber	}
51235537Sgber
52235537Sgber	if ((out = param_get_string(params, "out"))) {
53235537Sgber		out_fd = open(out, O_WRONLY|O_CREAT);
54235537Sgber		if (out_fd == -1) {
55235537Sgber			perrorf("Cannot open %s for writing", out);
56235537Sgber			return (1);
57235537Sgber		}
58235537Sgber	}
59235537Sgber
60235537Sgber	if ((fd = g_open(dev, 1)) == -1) {
61235537Sgber		perrorf("Cannot open %s", dev);
62235537Sgber		ret = 1;
63235537Sgber		goto out;
64235537Sgber	}
65235537Sgber
66235537Sgber	if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) == -1) {
67235537Sgber		perrorf("Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
68235537Sgber		ret = 1;
69235537Sgber		goto out;
70235537Sgber	}
71235537Sgber
72235537Sgber	block_size = chip_params.page_size * chip_params.pages_per_block;
73235537Sgber
74235537Sgber	if (param_has_value(params, "page")) {
75235537Sgber		pos = chip_params.page_size * param_get_int(params, "page");
76235537Sgber		mult = chip_params.page_size;
77235537Sgber	} else if (param_has_value(params, "block")) {
78235537Sgber		pos = block_size * param_get_int(params, "block");
79235537Sgber		mult = block_size;
80235537Sgber	} else if (param_has_value(params, "pos")) {
81235537Sgber		pos = param_get_int(params, "pos");
82235537Sgber		mult = 1;
83235537Sgber		if (pos % chip_params.page_size) {
84235537Sgber			fprintf(stderr, "Position must be page-size aligned!\n");
85235537Sgber			ret = 1;
86235537Sgber			goto out;
87235537Sgber		}
88235537Sgber	} else {
89235537Sgber		fprintf(stderr, "You must specify one of: 'block', 'page',"
90235537Sgber		    "'pos' arguments\n");
91235537Sgber		ret = 1;
92235537Sgber		goto out;
93235537Sgber	}
94235537Sgber
95235537Sgber	if (!(param_has_value(params, "count")))
96235537Sgber		count = mult;
97235537Sgber	else
98235537Sgber		count = param_get_int(params, "count") * mult;
99235537Sgber
100235537Sgber	if (!(buf = malloc(chip_params.page_size))) {
101235537Sgber		perrorf("Cannot allocate buffer [size %x]",
102235537Sgber		    chip_params.page_size);
103235537Sgber		ret = 1;
104235537Sgber		goto out;
105235537Sgber	}
106235537Sgber
107235537Sgber	lseek(fd, pos, SEEK_SET);
108235537Sgber
109235537Sgber	while (done < count) {
110235537Sgber		if ((ret = read(fd, buf, chip_params.page_size)) !=
111235537Sgber		    (int32_t)chip_params.page_size) {
112235537Sgber			perrorf("read error (read %d bytes)", ret);
113235537Sgber			goto out;
114235537Sgber		}
115235537Sgber
116235537Sgber		if (out_fd != -1) {
117235537Sgber			done += ret;
118235537Sgber			if ((ret = write(out_fd, buf, chip_params.page_size)) !=
119235537Sgber			    (int32_t)chip_params.page_size) {
120235537Sgber				perrorf("write error (written %d bytes)", ret);
121235537Sgber				ret = 1;
122235537Sgber				goto out;
123235537Sgber			}
124235537Sgber		} else {
125235537Sgber			hexdumpoffset(buf, chip_params.page_size, done);
126235537Sgber			done += ret;
127235537Sgber		}
128235537Sgber	}
129235537Sgber
130235537Sgberout:
131235537Sgber	g_close(fd);
132235537Sgber	if (out_fd != -1)
133235537Sgber		close(out_fd);
134235537Sgber	if (buf)
135235537Sgber		free(buf);
136235537Sgber
137235537Sgber	return (ret);
138235537Sgber}
139235537Sgber
140