summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2018-04-10 16:22:13 -0500
committerElliott Hughes2018-04-10 16:24:02 -0500
commit6ebec932d65f0f716cad784e825051f18acb5165 (patch)
tree3b50ed1bcea6e3bcf67af1966ef68bc8e291020c /fastboot
parent20d21598033bb682a54ec1eca2568e030ad92e46 (diff)
downloadplatform-system-core-6ebec932d65f0f716cad784e825051f18acb5165.tar.gz
platform-system-core-6ebec932d65f0f716cad784e825051f18acb5165.tar.xz
platform-system-core-6ebec932d65f0f716cad784e825051f18acb5165.zip
Add fastboot_test.cpp and test --os-version/--os-patch-level parsing.
Also switch all remaining headers over to #pragma once, and actually use FB_COMMAND_SZ. Bug: http://b/77340848 Test: ran tests Change-Id: I27107d054c206e66c39208099e36a55df604e08f
Diffstat (limited to 'fastboot')
-rw-r--r--fastboot/Android.mk8
-rw-r--r--fastboot/fastboot.cpp42
-rw-r--r--fastboot/fastboot.h10
-rw-r--r--fastboot/fastboot_test.cpp61
-rw-r--r--fastboot/fs.h5
-rw-r--r--fastboot/protocol.cpp6
-rw-r--r--fastboot/socket.h5
-rw-r--r--fastboot/socket_mock.h5
-rw-r--r--fastboot/tcp.h5
-rw-r--r--fastboot/transport.h5
-rw-r--r--fastboot/udp.h5
-rw-r--r--fastboot/usb.h5
12 files changed, 108 insertions, 54 deletions
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index a142b3ebc..5321b4f24 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -83,6 +83,7 @@ LOCAL_MODULE_HOST_OS := darwin linux windows
83LOCAL_CFLAGS := $(fastboot_cflags) 83LOCAL_CFLAGS := $(fastboot_cflags)
84LOCAL_CFLAGS_darwin := $(fastboot_cflags_darwin) 84LOCAL_CFLAGS_darwin := $(fastboot_cflags_darwin)
85LOCAL_CXX_STL := $(fastboot_stl) 85LOCAL_CXX_STL := $(fastboot_stl)
86LOCAL_HEADER_LIBRARIES := bootimg_headers
86LOCAL_LDLIBS_darwin := $(fastboot_ldflags_darwin) 87LOCAL_LDLIBS_darwin := $(fastboot_ldflags_darwin)
87LOCAL_LDLIBS_windows := $(fastboot_ldlibs_windows) 88LOCAL_LDLIBS_windows := $(fastboot_ldlibs_windows)
88LOCAL_REQUIRED_MODULES := mke2fs make_f2fs 89LOCAL_REQUIRED_MODULES := mke2fs make_f2fs
@@ -93,8 +94,6 @@ LOCAL_SRC_FILES := main.cpp
93LOCAL_SHARED_LIBRARIES := $(fastboot_shared_libs) 94LOCAL_SHARED_LIBRARIES := $(fastboot_shared_libs)
94LOCAL_SHARED_LIBRARIES_windows := AdbWinApi 95LOCAL_SHARED_LIBRARIES_windows := AdbWinApi
95LOCAL_STATIC_LIBRARIES := libfastboot $(fastboot_static_libs) 96LOCAL_STATIC_LIBRARIES := libfastboot $(fastboot_static_libs)
96LOCAL_STATIC_LIBRARIES_darwin := libselinux
97LOCAL_STATIC_LIBRARIES_linux := libselinux
98include $(BUILD_HOST_EXECUTABLE) 97include $(BUILD_HOST_EXECUTABLE)
99 98
100# 99#
@@ -120,8 +119,10 @@ my_dist_files :=
120include $(CLEAR_VARS) 119include $(CLEAR_VARS)
121LOCAL_MODULE := fastboot_test 120LOCAL_MODULE := fastboot_test
122LOCAL_MODULE_HOST_OS := darwin linux windows 121LOCAL_MODULE_HOST_OS := darwin linux windows
122LOCAL_MODULE_HOST_CROSS_ARCH := x86 # Avoid trying to build for win64.
123 123
124LOCAL_SRC_FILES := \ 124LOCAL_SRC_FILES := \
125 fastboot_test.cpp \
125 socket_mock.cpp \ 126 socket_mock.cpp \
126 socket_test.cpp \ 127 socket_test.cpp \
127 tcp_test.cpp \ 128 tcp_test.cpp \
@@ -130,7 +131,10 @@ LOCAL_SRC_FILES := \
130LOCAL_CFLAGS := $(fastboot_cflags) 131LOCAL_CFLAGS := $(fastboot_cflags)
131LOCAL_CFLAGS_darwin := $(fastboot_cflags_darwin) 132LOCAL_CFLAGS_darwin := $(fastboot_cflags_darwin)
132LOCAL_CXX_STL := $(fastboot_stl) 133LOCAL_CXX_STL := $(fastboot_stl)
134LOCAL_HEADER_LIBRARIES := bootimg_headers
133LOCAL_LDLIBS_darwin := $(fastboot_ldflags_darwin) 135LOCAL_LDLIBS_darwin := $(fastboot_ldflags_darwin)
134LOCAL_LDLIBS_windows := $(fastboot_ldlibs_windows) 136LOCAL_LDLIBS_windows := $(fastboot_ldlibs_windows)
137LOCAL_SHARED_LIBRARIES := $(fastboot_shared_libs)
138LOCAL_SHARED_LIBRARIES_windows := AdbWinApi
135LOCAL_STATIC_LIBRARIES := libfastboot $(fastboot_static_libs) 139LOCAL_STATIC_LIBRARIES := libfastboot $(fastboot_static_libs)
136include $(BUILD_HOST_NATIVE_TEST) 140include $(BUILD_HOST_NATIVE_TEST)
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index 42f4fbb4b..5666fa1e9 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -1475,24 +1475,9 @@ int FastBoot::Main(int argc, char* argv[]) {
1475 } else if (name == "kernel-offset") { 1475 } else if (name == "kernel-offset") {
1476 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16); 1476 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1477 } else if (name == "os-patch-level") { 1477 } else if (name == "os-patch-level") {
1478 unsigned year, month, day; 1478 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
1479 if (sscanf(optarg, "%u-%u-%u", &year, &month, &day) != 3) {
1480 syntax_error("OS patch level should be YYYY-MM-DD: %s", optarg);
1481 }
1482 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1483 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1484 g_boot_img_hdr.SetOsPatchLevel(year, month);
1485 } else if (name == "os-version") { 1479 } else if (name == "os-version") {
1486 unsigned major = 0, minor = 0, patch = 0; 1480 ParseOsVersion(&g_boot_img_hdr, optarg);
1487 std::vector<std::string> versions = android::base::Split(optarg, ".");
1488 if (versions.size() < 1 || versions.size() > 3 ||
1489 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1490 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1491 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1492 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1493 syntax_error("bad OS version: %s", optarg);
1494 }
1495 g_boot_img_hdr.SetOsVersion(major, minor, patch);
1496 } else if (name == "page-size") { 1481 } else if (name == "page-size") {
1497 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0); 1482 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1498 if (g_boot_img_hdr.page_size == 0) die("invalid page size"); 1483 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
@@ -1801,3 +1786,26 @@ int FastBoot::Main(int argc, char* argv[]) {
1801 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start)); 1786 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1802 return status; 1787 return status;
1803} 1788}
1789
1790void FastBoot::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
1791 unsigned year, month, day;
1792 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1793 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1794 }
1795 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1796 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1797 hdr->SetOsPatchLevel(year, month);
1798}
1799
1800void FastBoot::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
1801 unsigned major = 0, minor = 0, patch = 0;
1802 std::vector<std::string> versions = android::base::Split(arg, ".");
1803 if (versions.size() < 1 || versions.size() > 3 ||
1804 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1805 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1806 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1807 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1808 syntax_error("bad OS version: %s", arg);
1809 }
1810 hdr->SetOsVersion(major, minor, patch);
1811}
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index 3d5a26130..2935eb57c 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -26,14 +26,15 @@
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#ifndef _FASTBOOT_H_ 29#pragma once
30#define _FASTBOOT_H_
31 30
32#include <inttypes.h> 31#include <inttypes.h>
33#include <stdlib.h> 32#include <stdlib.h>
34 33
35#include <string> 34#include <string>
36 35
36#include <bootimg.h>
37
37class Transport; 38class Transport;
38struct sparse_file; 39struct sparse_file;
39 40
@@ -99,6 +100,7 @@ extern char cur_product[FB_RESPONSE_SZ + 1];
99class FastBoot { 100class FastBoot {
100 public: 101 public:
101 int Main(int argc, char* argv[]); 102 int Main(int argc, char* argv[]);
102};
103 103
104#endif 104 void ParseOsPatchLevel(boot_img_hdr_v1*, const char*);
105 void ParseOsVersion(boot_img_hdr_v1*, const char*);
106};
diff --git a/fastboot/fastboot_test.cpp b/fastboot/fastboot_test.cpp
new file mode 100644
index 000000000..1681427c7
--- /dev/null
+++ b/fastboot/fastboot_test.cpp
@@ -0,0 +1,61 @@
1/*
2 * Copyright (C) 2018 The Android Open Source Project
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
17#include "fastboot.h"
18
19#include <gtest/gtest.h>
20
21TEST(FastBoot, ParseOsPatchLevel) {
22 FastBoot fb;
23 boot_img_hdr_v1 hdr;
24
25 hdr = {};
26 fb.ParseOsPatchLevel(&hdr, "2018-01-05");
27 ASSERT_EQ(2018U, 2000U + ((hdr.os_version >> 4) & 0x7f));
28 ASSERT_EQ(1U, ((hdr.os_version >> 0) & 0xf));
29
30 EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018"), "should be YYYY-MM-DD");
31 EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-01"), "should be YYYY-MM-DD");
32 EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2128-01-05"), "year out of range");
33 EXPECT_DEATH(fb.ParseOsPatchLevel(&hdr, "2018-13-05"), "month out of range");
34}
35
36TEST(FastBoot, ParseOsVersion) {
37 FastBoot fb;
38 boot_img_hdr_v1 hdr;
39
40 hdr = {};
41 fb.ParseOsVersion(&hdr, "1.2.3");
42 ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
43 ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f));
44 ASSERT_EQ(3U, ((hdr.os_version >> 11) & 0x7f));
45
46 fb.ParseOsVersion(&hdr, "1.2");
47 ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
48 ASSERT_EQ(2U, ((hdr.os_version >> 18) & 0x7f));
49 ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f));
50
51 fb.ParseOsVersion(&hdr, "1");
52 ASSERT_EQ(1U, ((hdr.os_version >> 25) & 0x7f));
53 ASSERT_EQ(0U, ((hdr.os_version >> 18) & 0x7f));
54 ASSERT_EQ(0U, ((hdr.os_version >> 11) & 0x7f));
55
56 EXPECT_DEATH(fb.ParseOsVersion(&hdr, ""), "bad OS version");
57 EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.3.4"), "bad OS version");
58 EXPECT_DEATH(fb.ParseOsVersion(&hdr, "128.2.3"), "bad OS version");
59 EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.128.3"), "bad OS version");
60 EXPECT_DEATH(fb.ParseOsVersion(&hdr, "1.2.128"), "bad OS version");
61}
diff --git a/fastboot/fs.h b/fastboot/fs.h
index c6baa7f67..331100de1 100644
--- a/fastboot/fs.h
+++ b/fastboot/fs.h
@@ -1,5 +1,4 @@
1#ifndef _FS_H_ 1#pragma once
2#define _FS_H_
3 2
4#include <string> 3#include <string>
5#include <stdint.h> 4#include <stdint.h>
@@ -9,5 +8,3 @@ struct fs_generator;
9const struct fs_generator* fs_get_generator(const std::string& fs_type); 8const struct fs_generator* fs_get_generator(const std::string& fs_type);
10int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize, 9int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
11 const std::string& initial_dir, unsigned eraseBlkSize = 0, unsigned logicalBlkSize = 0); 10 const std::string& initial_dir, unsigned eraseBlkSize = 0, unsigned logicalBlkSize = 0);
12
13#endif
diff --git a/fastboot/protocol.cpp b/fastboot/protocol.cpp
index 133a2d0f2..8c20262e5 100644
--- a/fastboot/protocol.cpp
+++ b/fastboot/protocol.cpp
@@ -57,10 +57,10 @@ const std::string fb_get_error() {
57} 57}
58 58
59static int64_t check_response(Transport* transport, uint32_t size, char* response) { 59static int64_t check_response(Transport* transport, uint32_t size, char* response) {
60 char status[65]; 60 char status[FB_RESPONSE_SZ + 1];
61 61
62 while (true) { 62 while (true) {
63 int r = transport->Read(status, 64); 63 int r = transport->Read(status, FB_RESPONSE_SZ);
64 if (r < 0) { 64 if (r < 0) {
65 g_error = android::base::StringPrintf("status read failed (%s)", strerror(errno)); 65 g_error = android::base::StringPrintf("status read failed (%s)", strerror(errno));
66 transport->Close(); 66 transport->Close();
@@ -120,7 +120,7 @@ static int64_t check_response(Transport* transport, uint32_t size, char* respons
120 120
121static int64_t _command_start(Transport* transport, const std::string& cmd, uint32_t size, 121static int64_t _command_start(Transport* transport, const std::string& cmd, uint32_t size,
122 char* response) { 122 char* response) {
123 if (cmd.size() > 64) { 123 if (cmd.size() > FB_COMMAND_SZ) {
124 g_error = android::base::StringPrintf("command too large (%zu)", cmd.size()); 124 g_error = android::base::StringPrintf("command too large (%zu)", cmd.size());
125 return -1; 125 return -1;
126 } 126 }
diff --git a/fastboot/socket.h b/fastboot/socket.h
index 7eaa0ab2c..e791f2c8b 100644
--- a/fastboot/socket.h
+++ b/fastboot/socket.h
@@ -30,8 +30,7 @@
30// engine should not be using this interface directly, but instead should use a higher-level 30// engine should not be using this interface directly, but instead should use a higher-level
31// interface that enforces the fastboot protocol. 31// interface that enforces the fastboot protocol.
32 32
33#ifndef SOCKET_H_ 33#pragma once
34#define SOCKET_H_
35 34
36#include <functional> 35#include <functional>
37#include <memory> 36#include <memory>
@@ -125,5 +124,3 @@ class Socket {
125 124
126 DISALLOW_COPY_AND_ASSIGN(Socket); 125 DISALLOW_COPY_AND_ASSIGN(Socket);
127}; 126};
128
129#endif // SOCKET_H_
diff --git a/fastboot/socket_mock.h b/fastboot/socket_mock.h
index eacd6bb6a..6e95b1602 100644
--- a/fastboot/socket_mock.h
+++ b/fastboot/socket_mock.h
@@ -26,8 +26,7 @@
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#ifndef SOCKET_MOCK_H_ 29#pragma once
30#define SOCKET_MOCK_H_
31 30
32#include <memory> 31#include <memory>
33#include <queue> 32#include <queue>
@@ -97,5 +96,3 @@ class SocketMock : public Socket {
97 96
98 DISALLOW_COPY_AND_ASSIGN(SocketMock); 97 DISALLOW_COPY_AND_ASSIGN(SocketMock);
99}; 98};
100
101#endif // SOCKET_MOCK_H_
diff --git a/fastboot/tcp.h b/fastboot/tcp.h
index aa3ef13d9..8b638a418 100644
--- a/fastboot/tcp.h
+++ b/fastboot/tcp.h
@@ -26,8 +26,7 @@
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#ifndef TCP_H_ 29#pragma once
30#define TCP_H_
31 30
32#include <memory> 31#include <memory>
33#include <string> 32#include <string>
@@ -55,5 +54,3 @@ std::unique_ptr<Transport> Connect(std::unique_ptr<Socket> sock, std::string* er
55} // namespace internal 54} // namespace internal
56 55
57} // namespace tcp 56} // namespace tcp
58
59#endif // TCP_H_
diff --git a/fastboot/transport.h b/fastboot/transport.h
index 67d01f95e..96b90d2fe 100644
--- a/fastboot/transport.h
+++ b/fastboot/transport.h
@@ -14,8 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#ifndef TRANSPORT_H_ 17#pragma once
18#define TRANSPORT_H_
19 18
20#include <android-base/macros.h> 19#include <android-base/macros.h>
21 20
@@ -44,5 +43,3 @@ class Transport {
44 private: 43 private:
45 DISALLOW_COPY_AND_ASSIGN(Transport); 44 DISALLOW_COPY_AND_ASSIGN(Transport);
46}; 45};
47
48#endif // TRANSPORT_H_
diff --git a/fastboot/udp.h b/fastboot/udp.h
index 14f5b3547..8d37b841d 100644
--- a/fastboot/udp.h
+++ b/fastboot/udp.h
@@ -26,8 +26,7 @@
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#ifndef UDP_H_ 29#pragma once
30#define UDP_H_
31 30
32#include <memory> 31#include <memory>
33#include <string> 32#include <string>
@@ -77,5 +76,3 @@ std::unique_ptr<Transport> Connect(std::unique_ptr<Socket> sock, std::string* er
77} // namespace internal 76} // namespace internal
78 77
79} // namespace udp 78} // namespace udp
80
81#endif // UDP_H_
diff --git a/fastboot/usb.h b/fastboot/usb.h
index 4acf12d57..5b44468bb 100644
--- a/fastboot/usb.h
+++ b/fastboot/usb.h
@@ -26,8 +26,7 @@
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#ifndef _USB_H_ 29#pragma once
30#define _USB_H_
31 30
32#include "transport.h" 31#include "transport.h"
33 32
@@ -56,5 +55,3 @@ struct usb_ifc_info {
56typedef int (*ifc_match_func)(usb_ifc_info *ifc); 55typedef int (*ifc_match_func)(usb_ifc_info *ifc);
57 56
58Transport* usb_open(ifc_match_func callback); 57Transport* usb_open(ifc_match_func callback);
59
60#endif