Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Kra1o5 | 3167936 | 2014-06-20 19:06:06 +0200 | [diff] [blame] | 17 | /* This is a hack for Rockchip rk3x based devices. The problem is that |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 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 | |
Kra1o5 | 3167936 | 2014-06-20 19:06:06 +0200 | [diff] [blame] | 35 | #include "rk3xhack.h" |
Sergey 'Jin' Bostandzhyan | 80a90ed | 2013-01-04 02:29:03 +0100 | [diff] [blame] | 36 | |
| 37 | int 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 | } |