blob: 930f60593a20477500dd2a87b594ce49dc82715b [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
Kra1o531679362014-06-20 19:06:06 +020017/* This is a hack for Rockchip rk3x based devices. The problem is that
Sergey 'Jin' Bostandzhyan80a90ed2013-01-04 02:29:03 +010018 * 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
Kra1o531679362014-06-20 19:06:06 +020035#include "rk3xhack.h"
Sergey 'Jin' Bostandzhyan80a90ed2013-01-04 02:29:03 +010036
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}