part_emmc_ufs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2017. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <pal_typedefs.h>
  36. #include <pal_assert.h>
  37. #include <storage_api.h>
  38. #include <partition_api.h>
  39. #include <partition_error.h>
  40. #include <partition_active.h>
  41. #include <platform.h>
  42. #include <boot_device.h>
  43. #include <partition.h>
  44. /* not exported to user */
  45. #define PARTITION_OP_READ (0)
  46. #define PARTITION_OP_WRITE (1)
  47. static ssize_t partition_op(uint32_t op, char *part_name, uint64_t offset,
  48. uint8_t *buf, uint32_t size)
  49. {
  50. ssize_t len = 0;
  51. uint32_t ret = STATUS_OK;
  52. uint32_t blk_sz = 0x0;
  53. uint64_t src = 0x0ULL;
  54. part_t *part = NULL;
  55. if (NULL == (part = part_get(part_name))) {
  56. set_last_error(ERR_PART_GENERAL_NOT_EXIST);
  57. len = -1;
  58. goto end;
  59. }
  60. ret = storage_get_block_size(&blk_sz);
  61. if (ret) {
  62. set_last_error(ret);
  63. len = -1;
  64. goto end;
  65. }
  66. src = (uint64_t)part->start_sect * blk_sz + offset;
  67. /* currently we support USER region access only, to access other physical regions,
  68. you need partition name to physical region translation logic here */
  69. if (op == PARTITION_OP_READ) {
  70. len = storage_read(STORAGE_PHYS_PART_USER, src, buf, size);
  71. if (len != size)
  72. goto end;
  73. } else if (op == PARTITION_OP_WRITE) {
  74. len = storage_write(STORAGE_PHYS_PART_USER, src, buf, size);
  75. if (len != size)
  76. goto end;
  77. } else {
  78. /* unknown partition operation */
  79. set_last_error(ERR_PART_GENERAL_UNKNOWN_OP);
  80. len = -1;
  81. }
  82. end:
  83. return len;
  84. }
  85. ssize_t partition_read(char *part_name, uint64_t offset, uint8_t *buf,
  86. uint32_t size)
  87. {
  88. ssize_t len = 0;
  89. uint32_t ret = STATUS_OK;
  90. char active_part_name[PART_NAME_BUF_SZ] = {0};
  91. uint32_t input_part_name_sz = 0;
  92. if (NULL == part_name) {
  93. set_last_error(ERR_PART_GENERAL_INVALID_NAME);
  94. return -1;
  95. }
  96. input_part_name_sz = strlen(part_name);
  97. if (input_part_name_sz > (PART_NAME_BUF_SZ - 1)) {
  98. set_last_error(ERR_PART_GENERAL_INVALID_NAME_SZ);
  99. return -1;
  100. }
  101. memcpy(active_part_name, part_name, input_part_name_sz);
  102. /* name translation logic here to get active partition name */
  103. /* gpt active and ab system are mutually exclusive */
  104. #if defined (MTK_AB_OTA_UPDATER)
  105. ret = partition_get_ab(active_part_name, PART_NAME_BUF_SZ, GET_ACTIVE);
  106. if (ret) {
  107. set_last_error(ret);
  108. len = -1;
  109. goto end;
  110. }
  111. #else
  112. ret = partition_get_gpt_active(active_part_name, PART_NAME_BUF_SZ, GET_ACTIVE);
  113. if (ret) {
  114. set_last_error(ret);
  115. len = -1;
  116. goto end;
  117. }
  118. #endif
  119. len = partition_op(PARTITION_OP_READ, active_part_name, offset, buf, size);
  120. if (len != size)
  121. goto end;
  122. end:
  123. return len;
  124. }
  125. ssize_t partition_write(char *part_name, uint64_t offset, uint8_t *buf,
  126. uint32_t size)
  127. {
  128. ssize_t len = 0;
  129. uint32_t ret = STATUS_OK;
  130. char active_part_name[PART_NAME_BUF_SZ] = {0};
  131. uint32_t input_part_name_sz = 0;
  132. if (NULL == part_name) {
  133. set_last_error(ERR_PART_GENERAL_INVALID_NAME);
  134. return -1;
  135. }
  136. input_part_name_sz = strlen(part_name);
  137. if (input_part_name_sz > (PART_NAME_BUF_SZ - 1)) {
  138. set_last_error(ERR_PART_GENERAL_INVALID_NAME_SZ);
  139. return -1;
  140. }
  141. memcpy(active_part_name, part_name, input_part_name_sz);
  142. /* name translation logic here to get active partition name */
  143. /* gpt active and ab system are mutually exclusive */
  144. #if defined (MTK_AB_OTA_UPDATER)
  145. ret = partition_get_ab(active_part_name, PART_NAME_BUF_SZ, GET_ACTIVE);
  146. if (ret) {
  147. set_last_error(ret);
  148. len = -1;
  149. goto end;
  150. }
  151. #else
  152. ret = partition_get_gpt_active(active_part_name, PART_NAME_BUF_SZ, GET_ACTIVE);
  153. if (ret) {
  154. set_last_error(ret);
  155. len = -1;
  156. }
  157. #endif
  158. len = partition_op(PARTITION_OP_WRITE, active_part_name, offset, buf, size);
  159. if (len != size)
  160. goto end;
  161. end:
  162. return len;
  163. }
  164. ssize_t partition_read_inactive(char *part_name, uint64_t offset, uint8_t *buf,
  165. uint32_t size)
  166. {
  167. ssize_t len = 0;
  168. uint32_t ret = STATUS_OK;
  169. char inactive_part_name[PART_NAME_BUF_SZ] = {0};
  170. uint32_t input_part_name_sz = 0;
  171. if (NULL == part_name) {
  172. set_last_error(ERR_PART_GENERAL_INVALID_NAME);
  173. return -1;
  174. }
  175. input_part_name_sz = strlen(part_name);
  176. if (input_part_name_sz > (PART_NAME_BUF_SZ - 1)) {
  177. set_last_error(ERR_PART_GENERAL_INVALID_NAME_SZ);
  178. return -1;
  179. }
  180. memcpy(inactive_part_name, part_name, input_part_name_sz);
  181. /* name translation logic here to get active partition name */
  182. /* gpt active and ab system are mutually exclusive */
  183. #if defined (MTK_AB_OTA_UPDATER)
  184. ret = partition_get_ab(inactive_part_name, PART_NAME_BUF_SZ, GET_INACTIVE);
  185. if (ret) {
  186. set_last_error(ret);
  187. len = -1;
  188. goto end;
  189. }
  190. #else
  191. ret = partition_get_gpt_active(inactive_part_name, PART_NAME_BUF_SZ,
  192. GET_INACTIVE);
  193. if (ret) {
  194. set_last_error(ret);
  195. len = -1;
  196. goto end;
  197. }
  198. #endif
  199. len = partition_op(PARTITION_OP_READ, inactive_part_name, offset, buf, size);
  200. if (len != size)
  201. goto end;
  202. end:
  203. return len;
  204. }
  205. ssize_t partition_write_inactive(char *part_name, uint64_t offset, uint8_t *buf,
  206. uint32_t size)
  207. {
  208. ssize_t len = 0;
  209. uint32_t ret = STATUS_OK;
  210. char inactive_part_name[PART_NAME_BUF_SZ] = {0};
  211. uint32_t input_part_name_sz = 0;
  212. if (NULL == part_name) {
  213. set_last_error(ERR_PART_GENERAL_INVALID_NAME);
  214. return -1;
  215. }
  216. input_part_name_sz = strlen(part_name);
  217. if (input_part_name_sz > (PART_NAME_BUF_SZ - 1)) {
  218. set_last_error(ERR_PART_GENERAL_INVALID_NAME_SZ);
  219. return -1;
  220. }
  221. memcpy(inactive_part_name, part_name, input_part_name_sz);
  222. /* name translation logic here to get active partition name */
  223. /* gpt active and ab system are mutually exclusive */
  224. #if defined (MTK_AB_OTA_UPDATER)
  225. ret = partition_get_ab(inactive_part_name, PART_NAME_BUF_SZ, GET_INACTIVE);
  226. if (ret) {
  227. set_last_error(ret);
  228. len = -1;
  229. goto end;
  230. }
  231. #else
  232. ret = partition_get_gpt_active(inactive_part_name, PART_NAME_BUF_SZ,
  233. GET_INACTIVE);
  234. if (ret) {
  235. set_last_error(ret);
  236. len = -1;
  237. goto end;
  238. }
  239. #endif
  240. len = partition_op(PARTITION_OP_WRITE, inactive_part_name, offset, buf, size);
  241. if (len != size)
  242. goto end;
  243. end:
  244. return len;
  245. }