1 /*
2  * Copyright (C) 2014 Andrew Duggan
3  * Copyright (C) 2014 Synaptics Inc
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 #include <sys/types.h>
19 
20 #include "updateutil.h"
21 
22 const char *update_error_str[] = {
23 	"success",							// UPDATE_SUCCESS
24 	"failed",							// UPDATE_FAIL
25 	"timeout",							// UPDATE_FAIL_TIMEOUT
26 	"invalid firmware image",					// UPDATE_FAIL_VERIFY_IMAGE
27 	"checksum does not match image",				// UPDATE_FAIL_VERIFY_CHECKSUM
28 	"image firmware size does not match device",			// UPDATE_FAIL_VERIFY_FIRMWARE_SIZE
29 	"image config size does not match device",			// UPDATE_FAIL_VERIFY_CONFIG_SIZE
30 	"image version is unsupported",					// UPDATE_FAIL_UNSUPPORTED_IMAGE_VERSION
31 	"failed to find F01 on device",					// UPDATE_FAIL_NO_FUNCTION_01
32 	"failed to find F34 on device",					// UPDATE_FAIL_NO_FUNCTION_34
33 	"failed to query the basic properties in F01",			// UPDATE_FAIL_QUERY_BASIC_PROPERTIES
34 	"failed to read F34 query registers",				// UPDATE_FAIL_READ_F34_QUERIES
35 	"failed to read the bootloader id",				// UPDATE_FAIL_READ_BOOTLOADER_ID
36 	"failed to read F34 control registers",				// UPDATE_FAIL_READ_F34_CONTROLS
37 	"failed to write the bootloader id",				// UPDATE_FAIL_WRITE_BOOTLOADER_ID
38 	"failed to enable flash programming",				// UPDATE_FAIL_ENABLE_FLASH_PROGRAMMING
39 	"failed to reach idle state",					// UPDATE_FAIL_NOT_IN_IDLE_STATE
40 	"programming is not enabled",					// UPDATE_FAIL_PROGRAMMING_NOT_ENABLED
41 	"failed to scan the PDT",					// UPDATE_FAIL_SCAN_PDT
42 	"failed to read the device status",				// UPDATE_FAIL_READ_DEVICE_STATUS
43 	"device not in the bootloader after enabling programming",	// UPDATE_FAIL_DEVICE_NOT_IN_BOOTLOADER
44 	"failed to read F01 control 0 register",			// UPDATE_FAIL_READ_F01_CONTROL_0
45 	"failed to write F01 control 0 register",			// UPDATE_FAIL_WRITE_F01_CONTROL_0
46 	"failed to write initial zeros",				// UPDATE_FAIL_WRITE_INITIAL_ZEROS
47 	"failed to write block",					// UPDATE_FAIL_WRITE_BLOCK
48 	"failed to write the flash command",				// UPDATE_FAIL_WRITE_FLASH_COMMAND
49 	"timeout waiting for attn",					// UPDATE_FAIL_TIMEOUT_WAITING_FOR_ATTN
50 	"failed to write erase all command",				// UPDATE_FAIL_ERASE_ALL
51 	"the firmware image is older then the firmware on the device",	// UPDATE_FAIL_FIRMWARE_IMAGE_IS_OLDER
52 	"invalid parameter",						// UPDATE_FAIL_INVALID_PARAMETER
53 	"failed to open firmware image file",				// UPDATE_FAIL_OPEN_FIRMWARE_IMAGE
54 };
55 
update_err_to_string(int err)56 const char * update_err_to_string(int err)
57 {
58 	return update_error_str[err];
59 }
60 
extract_long(const unsigned char * data)61 unsigned long extract_long(const unsigned char *data)
62 {
63 	return (unsigned long)data [0]
64 		+ (unsigned long)data [1] * 0x100
65 		+ (unsigned long)data [2] * 0x10000
66 		+ (unsigned long)data [3] * 0x1000000;
67 }
68 
extract_short(const unsigned char * data)69 unsigned short extract_short(const unsigned char *data)
70 {
71 	return (unsigned long)data [0]
72 		+ (unsigned long)data [1] * 0x100;
73 }
74 
StripPath(const char * path,ssize_t size)75 const char * StripPath(const char * path, ssize_t size)
76 {
77 	int i;
78 	const char * str;
79 
80 	for (i = size - 1, str = &path[size - 1]; i > 0; --i, --str)
81 		if (path[i - 1] == '/')
82 			break;
83 
84 	return str;
85 }