blob: 4b6c1f792b961b4bf89b7d11955e46033f7a874f [file] [log] [blame]
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +11001/*
2 * Mailbox Daemon Flash Helpers
3 *
4 * Copyright 2016 IBM
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <getopt.h>
25#include <limits.h>
26#include <poll.h>
27#include <stdbool.h>
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <syslog.h>
33#include <signal.h>
34#include <sys/ioctl.h>
35#include <sys/mman.h>
36#include <sys/stat.h>
37#include <sys/timerfd.h>
38#include <sys/types.h>
39#include <time.h>
40#include <unistd.h>
41#include <inttypes.h>
42#include <mtd/mtd-abi.h>
43
44#include "mbox.h"
45#include "common.h"
46#include "mboxd_flash.h"
47
48int init_flash_dev(struct mbox_context *context)
49{
50 char *filename = get_dev_mtd();
51 int fd, rc = 0;
52
53 if (!filename) {
54 MSG_ERR("Couldn't find the PNOR /dev/mtd partition\n");
55 return -1;
56 }
57
Suraj Jitindar Singh28519592017-04-27 14:48:58 +100058 MSG_DBG("Opening %s\n", filename);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +110059
60 /* Open Flash Device */
61 fd = open(filename, O_RDWR);
62 if (fd < 0) {
63 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
64 filename, strerror(errno));
65 rc = -errno;
66 goto out;
67 }
68 context->fds[MTD_FD].fd = fd;
69
70 /* Read the Flash Info */
71 if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1) {
72 MSG_ERR("Couldn't get information about MTD: %s\n",
73 strerror(errno));
74 rc = -1;
75 goto out;
76 }
77
Andrew Jeffery33640b32017-04-12 20:56:11 +093078 if (context->flash_size == 0) {
79 /*
80 * PNOR images for current OpenPOWER systems are at most 64MB
81 * despite the PNOR itself sometimes being as big as 128MB. To
82 * ensure the image read from the PNOR is exposed in the LPC
83 * address space at the location expected by the host firmware,
84 * it is required that the image size be used for
85 * context->flash_size, and not the size of the flash device.
86 *
87 * However, the test cases specify the flash size via special
88 * test APIs (controlling flash behaviour) which don't have
89 * access to the mbox context. Rather than requiring
90 * error-prone assignments in every test case, we instead rely
91 * on context->flash_size being set to the size reported by the
92 * MEMINFO ioctl().
93 *
94 * As this case should never be hit in production (i.e. outside
95 * the test environment), log an error. As a consequence, this
96 * error is expected in the test case output.
97 */
98 MSG_ERR("Flash size MUST be supplied on the commandline. However, continuing by assuming flash is %u bytes\n",
99 context->mtd_info.size);
100 context->flash_size = context->mtd_info.size;
101 }
102
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100103 /* We know the erase size so we can allocate the flash_erased bytemap */
104 context->erase_size_shift = log_2(context->mtd_info.erasesize);
105 context->flash_bmap = calloc(context->flash_size >>
106 context->erase_size_shift,
107 sizeof(*context->flash_bmap));
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000108 MSG_DBG("Flash erase size: 0x%.8x\n", context->mtd_info.erasesize);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100109
110out:
111 free(filename);
112 return rc;
113}
114
115void free_flash_dev(struct mbox_context *context)
116{
117 free(context->flash_bmap);
118 close(context->fds[MTD_FD].fd);
119}
120
121/* Flash Functions */
122
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100123/*
124 * flash_is_erased() - Check if an offset into flash is erased
125 * @context: The mbox context pointer
126 * @offset: The flash offset to check (bytes)
127 *
128 * Return: true if erased otherwise false
129 */
130static inline bool flash_is_erased(struct mbox_context *context,
131 uint32_t offset)
132{
133 return context->flash_bmap[offset >> context->erase_size_shift]
134 == FLASH_ERASED;
135}
136
137/*
138 * set_flash_bytemap() - Set the flash erased bytemap
139 * @context: The mbox context pointer
140 * @offset: The flash offset to set (bytes)
141 * @count: Number of bytes to set
142 * @val: Value to set the bytemap to
143 *
144 * The flash bytemap only tracks the erased status at the erase block level so
145 * this will update the erased state for an (or many) erase blocks
146 *
147 * Return: 0 if success otherwise negative error code
148 */
149int set_flash_bytemap(struct mbox_context *context, uint32_t offset,
150 uint32_t count, uint8_t val)
151{
152 if ((offset + count) > context->flash_size) {
153 return -MBOX_R_PARAM_ERROR;
154 }
155
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000156 MSG_DBG("Set flash bytemap @ 0x%.8x for 0x%.8x to %s\n",
157 offset, count, val ? "ERASED" : "DIRTY");
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100158 memset(context->flash_bmap + (offset >> context->erase_size_shift),
159 val,
160 align_up(count, 1 << context->erase_size_shift) >>
161 context->erase_size_shift);
162
163 return 0;
164}
165
166/*
167 * erase_flash() - Erase the flash
168 * @context: The mbox context pointer
169 * @offset: The flash offset to erase (bytes)
170 * @size: The number of bytes to erase
171 *
172 * Return: 0 on success otherwise negative error code
173 */
174int erase_flash(struct mbox_context *context, uint32_t offset, uint32_t count)
175{
176 const uint32_t erase_size = 1 << context->erase_size_shift;
177 struct erase_info_user erase_info = { 0 };
178 int rc;
179
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000180 MSG_DBG("Erase flash @ 0x%.8x for 0x%.8x\n", offset, count);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100181
182 /*
183 * We have an erased_bytemap for the flash so we want to avoid erasing
184 * blocks which we already know to be erased. Look for runs of blocks
185 * which aren't erased and erase the entire run at once to avoid how
186 * often we have to call the erase ioctl. If the block is already
187 * erased then there's nothing we need to do.
188 */
189 while (count) {
190 if (!flash_is_erased(context, offset)) { /* Need to erase */
191 if (!erase_info.length) { /* Start of not-erased run */
192 erase_info.start = offset;
193 }
194 erase_info.length += erase_size;
195 } else if (erase_info.length) { /* Already erased|end of run? */
196 /* Erase the previous run which just ended */
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000197 MSG_DBG("Erase flash @ 0x%.8x for 0x%.8x\n",
198 erase_info.start, erase_info.length);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100199 rc = ioctl(context->fds[MTD_FD].fd, MEMERASE,
200 &erase_info);
201 if (rc < 0) {
202 MSG_ERR("Couldn't erase flash at 0x%.8x\n",
203 erase_info.start);
204 return -MBOX_R_SYSTEM_ERROR;
205 }
206 /* Mark ERASED where we just erased */
207 set_flash_bytemap(context, erase_info.start,
208 erase_info.length, FLASH_ERASED);
209 erase_info.start = 0;
210 erase_info.length = 0;
211 }
212
213 offset += erase_size;
214 count -= erase_size;
215 }
216
217 if (erase_info.length) {
Suraj Jitindar Singh28519592017-04-27 14:48:58 +1000218 MSG_DBG("Erase flash @ 0x%.8x for 0x%.8x\n",
219 erase_info.start, erase_info.length);
Suraj Jitindar Singhe39c9162017-03-28 10:47:43 +1100220 rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, &erase_info);
221 if (rc < 0) {
222 MSG_ERR("Couldn't erase flash at 0x%.8x\n",
223 erase_info.start);
224 return -MBOX_R_SYSTEM_ERROR;
225 }
226 /* Mark ERASED where we just erased */
227 set_flash_bytemap(context, erase_info.start, erase_info.length,
228 FLASH_ERASED);
229 }
230
231 return 0;
232}