yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev()
[yaffs2.git] / direct / u-boot / fs / yaffs2 / yaffs_mtdif2.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Charles Manning <charles@aleph1.co.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /* mtd interface for YAFFS2 */
14
15 /* XXX U-BOOT XXX */
16 #include <common.h>
17 #include "asm/errno.h"
18
19 #include "yportenv.h"
20 #include "yaffs_trace.h"
21
22 #include "yaffs_mtdif2.h"
23
24 #include "linux/mtd/mtd.h"
25 #include "linux/types.h"
26 #include "linux/time.h"
27
28 #include "yaffs_trace.h"
29 #include "yaffs_packedtags2.h"
30 #include "string.h"
31
32 #define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
33 #define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
34
35
36 /* NB For use with inband tags....
37  * We assume that the data buffer is of size total_bytes_per_chunk so
38  * that we can also use it to load the tags.
39  */
40 int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
41                               const u8 *data,
42                               const struct yaffs_ext_tags *tags)
43 {
44         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
45         struct mtd_oob_ops ops;
46
47         int retval = 0;
48
49         loff_t addr;
50         u8 local_spare[128];
51
52         struct yaffs_packed_tags2 pt;
53
54         int packed_tags_size =
55             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
56         void *packed_tags_ptr =
57             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
58
59         yaffs_trace(YAFFS_TRACE_MTD,
60                 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
61                 nand_chunk, data, tags);
62
63         addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
64
65         /* For yaffs2 writing there must be both data and tags.
66          * If we're using inband tags, then the tags are stuffed into
67          * the end of the data buffer.
68          */
69         if (!data || !tags)
70                 BUG();
71         else if (dev->param.inband_tags) {
72                 struct yaffs_packed_tags2_tags_only *pt2tp;
73                 pt2tp =
74                     (struct yaffs_packed_tags2_tags_only *)(data +
75                                                         dev->
76                                                         data_bytes_per_chunk);
77                 yaffs_pack_tags2_tags_only(pt2tp, tags);
78         } else {
79                 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
80         }
81
82         ops.mode = MTD_OOB_AUTO;
83         ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
84         ops.len = dev->param.total_bytes_per_chunk;
85         ops.ooboffs = 0;
86         ops.datbuf = (u8 *) data;
87         ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
88         retval = mtd->write_oob(mtd, addr, &ops);
89
90         if (retval == 0)
91                 return YAFFS_OK;
92         else
93                 return YAFFS_FAIL;
94 }
95
96 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
97                              u8 *data, struct yaffs_ext_tags *tags)
98 {
99         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
100         u8 local_spare[128];
101         struct mtd_oob_ops ops;
102         size_t dummy;
103         int retval = 0;
104         int local_data = 0;
105         struct yaffs_packed_tags2 pt;
106         loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
107         int packed_tags_size =
108             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
109         void *packed_tags_ptr =
110             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
111
112         yaffs_trace(YAFFS_TRACE_MTD,
113                 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
114                 nand_chunk, data, tags);
115
116         if (dev->param.inband_tags) {
117
118                 if (!data) {
119                         local_data = 1;
120                         data = yaffs_get_temp_buffer(dev);
121                 }
122
123         }
124
125         if (dev->param.inband_tags || (data && !tags))
126                 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
127                                    &dummy, data);
128         else if (tags) {
129                 ops.mode = MTD_OOB_AUTO;
130                 ops.ooblen = packed_tags_size;
131                 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
132                 ops.ooboffs = 0;
133                 ops.datbuf = data;
134                 ops.oobbuf = local_spare;
135                 retval = mtd->read_oob(mtd, addr, &ops);
136         }
137
138         if (dev->param.inband_tags) {
139                 if (tags) {
140                         struct yaffs_packed_tags2_tags_only *pt2tp;
141                         pt2tp =
142                                 (struct yaffs_packed_tags2_tags_only *)
143                                 &data[dev->data_bytes_per_chunk];
144                         yaffs_unpack_tags2_tags_only(tags, pt2tp);
145                 }
146         } else {
147                 if (tags) {
148                         memcpy(packed_tags_ptr,
149                                local_spare,
150                                packed_tags_size);
151                         yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
152                 }
153         }
154
155         if (local_data)
156                 yaffs_release_temp_buffer(dev, data);
157
158         if (tags && retval == -EBADMSG
159             && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
160                 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
161                 dev->n_ecc_unfixed++;
162         }
163         if (tags && retval == -EUCLEAN
164             && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
165                 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
166                 dev->n_ecc_fixed++;
167         }
168         if (retval == 0)
169                 return YAFFS_OK;
170         else
171                 return YAFFS_FAIL;
172 }
173
174
175 int nandmtd2_MarkNANDBlockBad(struct yaffs_dev *dev, int blockNo)
176 {
177         struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
178         int retval;
179
180         yaffs_trace(YAFFS_TRACE_MTD,
181                 "nandmtd2_MarkNANDBlockBad %d", blockNo);
182
183         retval =
184             mtd->block_markbad(mtd,
185                                blockNo * dev->param.chunks_per_block *
186                                dev->data_bytes_per_chunk);
187
188         if (retval == 0)
189                 return YAFFS_OK;
190         else
191                 return YAFFS_FAIL;
192
193 }
194
195 int nandmtd2_QueryNANDBlock(struct yaffs_dev *dev, int blockNo,
196                             enum yaffs_block_state *state, u32 *sequenceNumber)
197 {
198         struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
199         int retval;
200
201         yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_QueryNANDBlock %d", blockNo);
202         retval =
203             mtd->block_isbad(mtd,
204                              blockNo * dev->param.chunks_per_block *
205                              dev->data_bytes_per_chunk);
206
207         if (retval) {
208                 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
209
210                 *state = YAFFS_BLOCK_STATE_DEAD;
211                 *sequenceNumber = 0;
212         } else {
213                 struct yaffs_ext_tags t;
214                 nandmtd2_read_chunk_tags(dev,
215                                            blockNo *
216                                            dev->param.chunks_per_block, NULL,
217                                            &t);
218
219                 if (t.chunk_used) {
220                         *sequenceNumber = t.seq_number;
221                         *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
222                 } else {
223                         *sequenceNumber = 0;
224                         *state = YAFFS_BLOCK_STATE_EMPTY;
225                 }
226         }
227         yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d",
228                         *sequenceNumber, *state);
229
230         if (retval == 0)
231                 return YAFFS_OK;
232         else
233                 return YAFFS_FAIL;
234 }