blob: 5d0ee62676346051493dc0f69bb7c07badebae53 [file] [log] [blame]
San Mehata6391f12010-03-10 12:46:00 -08001/* libs/diskconfig/diskutils.c
2 *
3 * Copyright 2008, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "diskutils"
19
20#include <errno.h>
21#include <fcntl.h>
Mark Salyzyn1874e652014-03-13 16:38:43 -070022#include <inttypes.h>
San Mehata6391f12010-03-10 12:46:00 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/stat.h>
28
Mark Salyzyn1874e652014-03-13 16:38:43 -070029#include <log/log.h>
San Mehata6391f12010-03-10 12:46:00 -080030
31#include <diskconfig/diskconfig.h>
32
33int
34write_raw_image(const char *dst, const char *src, loff_t offset, int test)
35{
36 int dst_fd = -1;
37 int src_fd = -1;
38 uint8_t buffer[2048];
Mark Salyzyn1874e652014-03-13 16:38:43 -070039 ssize_t nr_bytes;
40 ssize_t tmp;
San Mehata6391f12010-03-10 12:46:00 -080041 int done = 0;
42 uint64_t total = 0;
43
Ying Wang9d4c76f2014-04-23 18:28:14 -070044 ALOGI("Writing RAW image '%s' to '%s' (offset=%llu)", src, dst, (unsigned long long)offset);
San Mehata6391f12010-03-10 12:46:00 -080045 if ((src_fd = open(src, O_RDONLY)) < 0) {
Steve Block01dda202012-01-06 14:13:42 +000046 ALOGE("Could not open %s for reading (errno=%d).", src, errno);
San Mehata6391f12010-03-10 12:46:00 -080047 goto fail;
48 }
49
50 if (!test) {
51 if ((dst_fd = open(dst, O_RDWR)) < 0) {
Steve Block01dda202012-01-06 14:13:42 +000052 ALOGE("Could not open '%s' for read/write (errno=%d).", dst, errno);
San Mehata6391f12010-03-10 12:46:00 -080053 goto fail;
54 }
55
56 if (lseek64(dst_fd, offset, SEEK_SET) != offset) {
Ying Wang9d4c76f2014-04-23 18:28:14 -070057 ALOGE("Could not seek to offset %lld in %s.", (long long)offset, dst);
San Mehata6391f12010-03-10 12:46:00 -080058 goto fail;
59 }
60 }
61
62 while (!done) {
63 if ((nr_bytes = read(src_fd, buffer, sizeof(buffer))) < 0) {
64 /* XXX: Should we not even bother with EINTR? */
65 if (errno == EINTR)
66 continue;
Steve Block01dda202012-01-06 14:13:42 +000067 ALOGE("Error (%d) while reading from '%s'", errno, src);
San Mehata6391f12010-03-10 12:46:00 -080068 goto fail;
69 }
70
71 if (!nr_bytes) {
72 /* we're done. */
73 done = 1;
74 break;
75 }
76
77 total += nr_bytes;
78
79 /* skip the write loop if we're testing */
80 if (test)
81 nr_bytes = 0;
82
83 while (nr_bytes > 0) {
84 if ((tmp = write(dst_fd, buffer, nr_bytes)) < 0) {
85 /* XXX: Should we not even bother with EINTR? */
86 if (errno == EINTR)
87 continue;
Steve Block01dda202012-01-06 14:13:42 +000088 ALOGE("Error (%d) while writing to '%s'", errno, dst);
San Mehata6391f12010-03-10 12:46:00 -080089 goto fail;
90 }
91 if (!tmp)
92 continue;
93 nr_bytes -= tmp;
94 }
95 }
96
97 if (!done) {
Steve Block01dda202012-01-06 14:13:42 +000098 ALOGE("Exited read/write loop without setting flag! WTF?!");
San Mehata6391f12010-03-10 12:46:00 -080099 goto fail;
100 }
101
102 if (dst_fd >= 0)
103 fsync(dst_fd);
104
Ying Wang9d4c76f2014-04-23 18:28:14 -0700105 ALOGI("Wrote %" PRIu64 " bytes to %s @ %lld", total, dst, (long long)offset);
San Mehata6391f12010-03-10 12:46:00 -0800106
107 close(src_fd);
108 if (dst_fd >= 0)
109 close(dst_fd);
110 return 0;
111
112fail:
113 if (dst_fd >= 0)
114 close(dst_fd);
115 if (src_fd >= 0)
116 close(src_fd);
117 return 1;
118}