Szymon Starzycki | b6c5f28 | 2013-07-24 17:08:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009-2013, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google, Inc. nor the names of its contributors |
| 15 | * may be used to endorse or promote products derived from this |
| 16 | * software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 25 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 26 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 28 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <sys/mman.h> |
| 35 | |
| 36 | #include "flash.h" |
| 37 | #include "protocol.h" |
| 38 | #include "debug.h" |
| 39 | #include "utils.h" |
| 40 | #include "commands/partitions.h" |
| 41 | |
| 42 | |
| 43 | #define ALLOWED_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-." |
| 44 | #define BUFFER_SIZE 1024 * 1024 |
| 45 | #define MIN(a, b) (a > b ? b : a) |
| 46 | |
| 47 | |
| 48 | int flash_find_entry(const char *name, char *out, size_t outlen) |
| 49 | { |
| 50 | //TODO: Assumption: All the partitions has they unique name |
| 51 | |
| 52 | const char *path = fastboot_getvar("device-directory"); |
| 53 | size_t length; |
| 54 | if (strcmp(path, "") == 0) { |
| 55 | D(ERR, "device-directory: not defined in config file"); |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | length = strspn(name, ALLOWED_CHARS); |
| 60 | if (length != strlen(name)) { |
| 61 | D(ERR, "Not allowed char in name: %c", name[length]); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | if (snprintf(out, outlen, "%s%s", path, name) >= (int) outlen) { |
| 66 | D(ERR, "Too long path to partition file"); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | if (access(out, F_OK ) == -1) { |
| 71 | D(ERR, "could not find partition file %s", name); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | int flash_erase(int fd) |
| 79 | { |
| 80 | int64_t size; |
| 81 | size = get_block_device_size(fd); |
| 82 | D(DEBUG, "erase %llu data from %d\n", size, fd); |
| 83 | |
| 84 | return wipe_block_device(fd, size); |
| 85 | } |
| 86 | |
| 87 | int flash_write(int partition_fd, int data_fd, ssize_t size, ssize_t skip) |
| 88 | { |
| 89 | ssize_t written = 0; |
| 90 | struct GPT_mapping input; |
| 91 | struct GPT_mapping output; |
| 92 | |
| 93 | while (written < size) { |
| 94 | int current_size = MIN(size - written, BUFFER_SIZE); |
| 95 | |
| 96 | if (gpt_mmap(&input, written + skip, current_size, data_fd)) { |
| 97 | D(ERR, "Error in writing data, unable to map data file %d at %d size %d", size, skip, current_size); |
| 98 | return -1; |
| 99 | } |
| 100 | if (gpt_mmap(&output, written, current_size, partition_fd)) { |
| 101 | D(ERR, "Error in writing data, unable to map output partition"); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | memcpy(output.ptr, input.ptr, current_size); |
| 106 | |
| 107 | gpt_unmap(&input); |
| 108 | gpt_unmap(&output); |
| 109 | |
| 110 | written += current_size; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |