blob: 8205af4aab6193e57397c9863bb8f6c761a3fadd [file] [log] [blame]
Szymon Starzyckie160f812013-07-24 17:08:04 -07001/*
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#include <sys/types.h>
32#include <sys/stat.h>
33#include <unistd.h>
34#include <stdio.h>
35#include <sys/ioctl.h>
36#include <linux/fs.h>
Szymon Starzycki5ccecfd2013-08-19 16:42:25 -070037#include <stdlib.h>
Szymon Starzyckie160f812013-07-24 17:08:04 -070038
39#include "utils.h"
40#include "debug.h"
41
42#ifndef BLKDISCARD
43#define BLKDISCARD _IO(0x12,119)
44#endif
45
46#ifndef BLKSECDISCARD
47#define BLKSECDISCARD _IO(0x12,125)
48#endif
49
50
51int get_stream_size(FILE *stream) {
52 int size;
53 fseek(stream, 0, SEEK_END);
54 size = ftell(stream);
55 fseek(stream, 0, SEEK_SET);
56 return size;
57}
58
59uint64_t get_block_device_size(int fd)
60{
61 uint64_t size = 0;
62 int ret;
63
64 ret = ioctl(fd, BLKGETSIZE64, &size);
65
66 if (ret)
67 return 0;
68
69 return size;
70}
71
72uint64_t get_file_size(int fd)
73{
74 struct stat buf;
75 int ret;
76 int64_t computed_size;
77
78 ret = fstat(fd, &buf);
79 if (ret)
80 return 0;
81
82 if (S_ISREG(buf.st_mode))
83 computed_size = buf.st_size;
84 else if (S_ISBLK(buf.st_mode))
85 computed_size = get_block_device_size(fd);
86 else
87 computed_size = 0;
88
89 return computed_size;
90}
91
92uint64_t get_file_size64(int fd)
93{
94 struct stat64 buf;
95 int ret;
96 uint64_t computed_size;
97
98 ret = fstat64(fd, &buf);
99 if (ret)
100 return 0;
101
102 if (S_ISREG(buf.st_mode))
103 computed_size = buf.st_size;
104 else if (S_ISBLK(buf.st_mode))
105 computed_size = get_block_device_size(fd);
106 else
107 computed_size = 0;
108
109 return computed_size;
110}
111
112
113char *strip(char *str)
114{
115 int n;
116
117 n = strspn(str, " \t");
118 str += n;
119 n = strcspn(str, " \t");
120 str[n] = '\0';
121
122 return str;
123}
124
125int wipe_block_device(int fd, int64_t len)
126{
127 uint64_t range[2];
128 int ret;
129
130 range[0] = 0;
131 range[1] = len;
132 ret = ioctl(fd, BLKSECDISCARD, &range);
133 if (ret < 0) {
134 range[0] = 0;
135 range[1] = len;
136 ret = ioctl(fd, BLKDISCARD, &range);
137 if (ret < 0) {
138 D(WARN, "Discard failed\n");
139 return 1;
140 } else {
141 D(WARN, "Wipe via secure discard failed, used discard instead\n");
142 return 0;
143 }
144 }
145
146 return 0;
147}
148
Szymon Starzycki5ccecfd2013-08-19 16:42:25 -0700149int create_temp_file() {
150 char tempname[] = "/dev/fastboot_data_XXXXXX";
151 int fd;
152
153 fd = mkstemp(tempname);
154 if (fd < 0)
155 return -1;
156
157 unlink(tempname);
158
159 return fd;
160}