blob: e9351b52e1875d373665b9b4d02b1c86d616fb78 [file] [log] [blame]
Sergey 'Jin' Bostandzhyan80a90ed2013-01-04 02:29:03 +01001/*
2 * Copyright (c) 2013, Sergey 'Jin' Bostandzhyan
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* This is a hack for Rockchip rk30xx based devices. The problem is that
18 * the MEMERASE ioctl is failing (hangs and never returns) in their kernel.
19 * The sources are not fully available, so fixing it in the rk30xxnand_ko driver
20 * is not possible.
21 *
22 * I straced the stock recovery application and it seems to avoid this
23 * particular ioctl, instead it is simply writing zeroes using the write() call.
24 *
25 * This workaround does the same and will replace all MEMERASE occurances in
26 * the recovery code.
27 */
28
29#include <sys/types.h>
30#include <unistd.h>
31#include <stdlib.h>
32#include <stdio.h>
33#include <errno.h>
34
35#include "rk30hack.h"
36
37int rk30_zero_out(int fd, off_t pos, ssize_t size)
38{
39 if (lseek(fd, pos, SEEK_SET) != pos) {
40 fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n",
41 pos, strerror(errno));
42 return -1;
43 }
44
45 unsigned char *zb = (unsigned char *)calloc(1, size);
46 if (zb == NULL) {
47 fprintf(stderr, "mtd: erase failure, could not allocate memory\n");
48 return -1;
49 }
50
51 if (write(fd, zb, size) != size) {
52 fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n",
53 pos, strerror(errno));
54 free(zb);
55 return -1;
56 }
57
58 free(zb);
59 return 0;
60}