fastboot.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * (C) Copyright 2008 - 2009
  3. * Windriver, <www.windriver.com>
  4. * Tom Rix <Tom.Rix@windriver.com>
  5. *
  6. * Copyright (C) 2010-2012 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. * The logical naming of flash comes from the Android project
  24. * Thse structures and functions that look like fastboot_flash_*
  25. * They come from bootloader/legacy/include/boot/flash.h
  26. *
  27. * The boot_img_hdr structure and associated magic numbers also
  28. * come from the Android project. They are from
  29. * system/core/mkbootimg/bootimg.h
  30. *
  31. * Here are their copyrights
  32. *
  33. * Copyright (C) 2008 The Android Open Source Project
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. * * Redistributions of source code must retain the above copyright
  40. * notice, this list of conditions and the following disclaimer.
  41. * * Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in
  43. * the documentation and/or other materials provided with the
  44. * distribution.
  45. *
  46. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  47. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  48. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  49. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  50. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  51. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  52. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  53. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  54. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  55. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  56. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  57. * SUCH DAMAGE.
  58. *
  59. */
  60. #ifndef FASTBOOT_H
  61. #define FASTBOOT_H
  62. #include <common.h>
  63. #include <command.h>
  64. /* This is the interface file between the common cmd_fastboot.c and
  65. the board specific support.
  66. To use this interface, define CONFIG_FASTBOOT in your board config file.
  67. An example is include/configs/omap3430labrador.h
  68. ...
  69. #define CONFIG_FASTBOOT 1 / * Using fastboot interface * /
  70. ...
  71. An example of the board specific spupport for omap3 is found at
  72. cpu/omap3/fastboot.c
  73. */
  74. /* From fastboot client.. */
  75. #define FASTBOOT_INTERFACE_CLASS 0xff
  76. #define FASTBOOT_INTERFACE_SUB_CLASS 0x42
  77. #define FASTBOOT_INTERFACE_PROTOCOL 0x03
  78. #define FASTBOOT_VERSION "0.5"
  79. /* The fastboot client uses a value of 2048 for the
  80. page size of it boot.img file format.
  81. Reset this in your board config file as needed. */
  82. #ifndef CFG_FASTBOOT_MKBOOTIMAGE_PAGE_SIZE
  83. #define CFG_FASTBOOT_MKBOOTIMAGE_PAGE_SIZE 2048
  84. #endif
  85. enum {
  86. DEV_SATA,
  87. DEV_MMC,
  88. DEV_NAND
  89. };
  90. struct cmd_fastboot_interface {
  91. /* This function is called when a buffer has been
  92. recieved from the client app.
  93. The buffer is a supplied by the board layer and must be unmodified.
  94. The buffer_size is how much data is passed in.
  95. Returns 0 on success
  96. Returns 1 on failure
  97. Set by cmd_fastboot */
  98. int (*rx_handler)(const unsigned char *buffer,
  99. unsigned int buffer_size);
  100. /* This function is called when an exception has
  101. occurred in the device code and the state
  102. off fastboot needs to be reset
  103. Set by cmd_fastboot */
  104. void (*reset_handler)(void);
  105. /* A getvar string for the product name
  106. It can have a maximum of 60 characters
  107. Set by board */
  108. char *product_name;
  109. /* A getvar string for the serial number
  110. It can have a maximum of 60 characters
  111. Set by board */
  112. char *serial_no;
  113. /* Nand block size
  114. Supports the write option WRITE_NEXT_GOOD_BLOCK
  115. Set by board */
  116. unsigned int nand_block_size;
  117. /* Nand oob size
  118. Set by board */
  119. unsigned int nand_oob_size;
  120. /* Transfer buffer, for handling flash updates
  121. Should be multiple of the nand_block_size
  122. Care should be take so it does not overrun bootloader memory
  123. Controlled by the configure variable CFG_FASTBOOT_TRANSFER_BUFFER
  124. Set by board */
  125. unsigned char *transfer_buffer;
  126. /* How big is the transfer buffer
  127. Controlled by the configure variable
  128. CFG_FASTBOOT_TRANSFER_BUFFER_SIZE
  129. Set by board */
  130. unsigned int transfer_buffer_size;
  131. };
  132. /* Android-style flash naming */
  133. typedef struct fastboot_ptentry fastboot_ptentry;
  134. /* flash partitions are defined in terms of blocks
  135. ** (flash erase units)
  136. */
  137. struct fastboot_ptentry {
  138. /* The logical name for this partition, null terminated */
  139. char name[16];
  140. /* The start wrt the nand part, must be multiple of nand block size */
  141. unsigned int start;
  142. /* The length of the partition, must be multiple of nand block size */
  143. unsigned int length;
  144. /* Controls the details of how operations are done on the partition
  145. See the FASTBOOT_PTENTRY_FLAGS_*'s defined below */
  146. unsigned int flags;
  147. /* partition id: 0 - normal partition; 1 - boot partition */
  148. unsigned int partition_id;
  149. };
  150. struct fastboot_device_info {
  151. unsigned char type;
  152. unsigned char dev_id;
  153. };
  154. /* Lower byte shows if the read/write/erase operation in
  155. repeated. The base address is incremented.
  156. Either 0 or 1 is ok for a default */
  157. #define FASTBOOT_PTENTRY_FLAGS_REPEAT(n) (n & 0x0f)
  158. #define FASTBOOT_PTENTRY_FLAGS_REPEAT_MASK 0x0000000F
  159. /* Writes happen a block at a time.
  160. If the write fails, go to next block
  161. NEXT_GOOD_BLOCK and CONTIGOUS_BLOCK can not both be set */
  162. #define FASTBOOT_PTENTRY_FLAGS_WRITE_NEXT_GOOD_BLOCK 0x00000010
  163. /* Find a contiguous block big enough for a the whole file
  164. NEXT_GOOD_BLOCK and CONTIGOUS_BLOCK can not both be set */
  165. #define FASTBOOT_PTENTRY_FLAGS_WRITE_CONTIGUOUS_BLOCK 0x00000020
  166. /* Sets the ECC to hardware before writing
  167. HW and SW ECC should not both be set. */
  168. #define FASTBOOT_PTENTRY_FLAGS_WRITE_HW_ECC 0x00000040
  169. /* Sets the ECC to software before writing
  170. HW and SW ECC should not both be set. */
  171. #define FASTBOOT_PTENTRY_FLAGS_WRITE_SW_ECC 0x00000080
  172. /* Write the file with write.i */
  173. #define FASTBOOT_PTENTRY_FLAGS_WRITE_I 0x00000100
  174. /* Write the file with write.yaffs */
  175. #define FASTBOOT_PTENTRY_FLAGS_WRITE_YAFFS 0x00000200
  176. /* Write the file as a series of variable/value pairs
  177. using the setenv and saveenv commands */
  178. #define FASTBOOT_PTENTRY_FLAGS_WRITE_ENV 0x00000400
  179. /* Status values */
  180. #define FASTBOOT_OK 0
  181. #define FASTBOOT_ERROR -1
  182. #define FASTBOOT_DISCONNECT 1
  183. #define FASTBOOT_INACTIVE 2
  184. /* Android bootimage file format */
  185. #define FASTBOOT_BOOT_MAGIC "ANDROID!"
  186. #define FASTBOOT_BOOT_MAGIC_SIZE 8
  187. #define FASTBOOT_BOOT_NAME_SIZE 16
  188. #define FASTBOOT_BOOT_ARGS_SIZE 512
  189. struct fastboot_boot_img_hdr {
  190. unsigned char magic[FASTBOOT_BOOT_MAGIC_SIZE];
  191. unsigned kernel_size; /* size in bytes */
  192. unsigned kernel_addr; /* physical load addr */
  193. unsigned ramdisk_size; /* size in bytes */
  194. unsigned ramdisk_addr; /* physical load addr */
  195. unsigned second_size; /* size in bytes */
  196. unsigned second_addr; /* physical load addr */
  197. unsigned tags_addr; /* physical addr for kernel tags */
  198. unsigned page_size; /* flash page size we assume */
  199. unsigned unused[2]; /* future expansion: should be 0 */
  200. unsigned char name[FASTBOOT_BOOT_NAME_SIZE]; /* asciiz product name */
  201. unsigned char cmdline[FASTBOOT_BOOT_ARGS_SIZE];
  202. unsigned id[8]; /* timestamp / checksum / sha1 / etc */
  203. };
  204. #ifdef CONFIG_FASTBOOT
  205. /* A board specific test if u-boot should go into the fastboot command
  206. ahead of the bootcmd
  207. Returns 0 to continue with normal u-boot flow
  208. Returns 1 to execute fastboot */
  209. int fastboot_preboot(void);
  210. /* Initizes the board specific fastboot
  211. Returns 0 on success
  212. Returns 1 on failure */
  213. int fastboot_init(struct cmd_fastboot_interface *interface);
  214. /* Cleans up the board specific fastboot */
  215. void fastboot_shutdown(void);
  216. /*
  217. * Handles board specific usb protocol exchanges
  218. * Returns 0 on success
  219. * Returns 1 on disconnects, break out of loop
  220. * Returns 2 if no USB activity detected
  221. * Returns -1 on failure, unhandled usb requests and other error conditions
  222. */
  223. int fastboot_poll(void);
  224. /* Is this high speed (2.0) or full speed (1.1) ?
  225. Returns 0 on full speed
  226. Returns 1 on high speed */
  227. int fastboot_is_highspeed(void);
  228. /* Return the size of the fifo */
  229. int fastboot_fifo_size(void);
  230. /* Send a status reply to the client app
  231. buffer does not have to be null terminated.
  232. buffer_size must be not be larger than what is returned by
  233. fastboot_fifo_size
  234. Returns 0 on success
  235. Returns 1 on failure */
  236. int fastboot_tx_status(const char *buffer, unsigned int buffer_size);
  237. /*
  238. * Send some data to the client app
  239. * buffer does not have to be null terminated.
  240. * buffer_size can be larger than what is returned by
  241. * fastboot_fifo_size
  242. * Returns number of bytes written
  243. */
  244. int fastboot_tx(unsigned char *buffer, unsigned int buffer_size);
  245. /* A board specific variable handler.
  246. The size of the buffers is governed by the fastboot spec.
  247. rx_buffer is at most 57 bytes
  248. tx_buffer is at most 60 bytes
  249. Returns 0 on success
  250. Returns 1 on failure */
  251. int fastboot_getvar(const char *rx_buffer, char *tx_buffer);
  252. /* The Android-style flash handling */
  253. /* tools to populate and query the partition table */
  254. void fastboot_flash_add_ptn(fastboot_ptentry *ptn);
  255. fastboot_ptentry *fastboot_flash_find_ptn(const char *name);
  256. fastboot_ptentry *fastboot_flash_get_ptn(unsigned n);
  257. unsigned int fastboot_flash_get_ptn_count(void);
  258. void fastboot_flash_dump_ptn(void);
  259. int fastboot_flash_init(void);
  260. int fastboot_flash_erase(fastboot_ptentry *ptn);
  261. int fastboot_flash_read_ext(fastboot_ptentry *ptn,
  262. unsigned extra_per_page, unsigned offset,
  263. void *data, unsigned bytes);
  264. #define fastboot_flash_read(ptn, offset, data, bytes) \
  265. flash_read_ext(ptn, 0, offset, data, bytes)
  266. int fastboot_flash_write(fastboot_ptentry *ptn, unsigned extra_per_page,
  267. const void *data, unsigned bytes);
  268. /* Check the board special boot mode reboot to fastboot mode. */
  269. int fastboot_check_and_clean_flag(void);
  270. int do_fastboot(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  271. void check_fastboot_mode(void);
  272. void fastboot_quick(u8 debug);
  273. int fastboot_write_storage(u8 *partition_name, u32 write_len);
  274. void fastboot_dump_memory(u32 *ptr, u32 lEN);
  275. void fastboot_get_ep_num(u8 *in, u8 *out);
  276. extern u8 fastboot_debug_level;
  277. #define DBG_ALWS(x...) printf(x)
  278. #define DBG_ERR(x...) printf(x)
  279. #define DBG_DEBUG(x...) if (fastboot_debug_level >= 1) printf(x)
  280. #define DBG_INFO(x...) if (fastboot_debug_level >= 2) printf(x)
  281. #else
  282. /* Stubs for when CONFIG_FASTBOOT is not defined */
  283. #define fastboot_preboot() 0
  284. #define fastboot_init(a) 1
  285. #define fastboot_shutdown()
  286. #define fastboot_poll() 1
  287. #define fastboot_is_highspeed() 0
  288. #define fastboot_fifo_size() 0
  289. #define fastboot_tx_status(a, b) 1
  290. #define fastboot_getvar(a, b) 1
  291. #define fastboot_tx(a, b) 1
  292. #define fastboot_flash_add_ptn(a)
  293. #define fastboot_flash_find_ptn(a) NULL
  294. #define fastboot_flash_get_ptn(a) NULL
  295. #define fastboot_flash_get_ptn_count() 0
  296. #define fastboot_flash_dump_ptn()
  297. #define fastboot_flash_init()
  298. #define fastboot_flash_erase(a) 1
  299. #define fastboot_flash_read_ext(a, b, c, d, e) 0
  300. #define fastboot_flash_read(a, b, c, d, e) 0
  301. #define fastboot_flash_write(a, b, c, d) 0
  302. #define do_fastboot(a, b, c, d) 0
  303. #define fastboot_quick(a) 0
  304. #define fastboot_get_ep_num(a, b) 0
  305. #define fastboot_dump_memory(a, b) 0
  306. #define DBG_ALWS(x...)
  307. #define DBG_ERR(x...)
  308. #define DBG_DEBUG(x...)
  309. #define DBG_INFO(x...)
  310. #endif /* CONFIG_FASTBOOT */
  311. #endif /* FASTBOOT_H */