blob: b848c7a36b744ef8853dfad2e5b92751e4618492 [file] [log] [blame]
Reut Zysmanff6bab92016-02-09 14:06:31 +02001/* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdlib.h>
30#include <stdio.h>
31#include <stdbool.h>
32#include <debug.h>
33#include <dev/fbcon.h>
34#include <target.h>
35#include <mmc.h>
36#include <partition_parser.h>
37#include <string.h>
38#include "mdtp.h"
39#include "mdtp_fs.h"
40
41/*---------------------------------------------------------
42 * Global Variables
43 *-------------------------------------------------------*/
44static mdtp_image_t mdtp_img;
45
46/*---------------------------------------------------------
47 * External Functions
48 *-------------------------------------------------------*/
49
50uint32_t get_image_offset(mdtp_image_id_t img){
51 return mdtp_img.meta_data.image_params[img].offset;
52}
53
54uint32_t get_image_width(mdtp_image_id_t img){
55 return mdtp_img.meta_data.image_params[img].width;
56}
57
58uint32_t get_image_height(mdtp_image_id_t img){
59 return mdtp_img.meta_data.image_params[img].height;
60}
61
62uint32_t mdtp_fs_get_param(mdtp_parameter_id_t param){
63 return mdtp_img.meta_data.params[param];
64}
65
66
67int mdtp_fs_init(){
68 int index = INVALID_PTN;
69 unsigned long long ptn = 0;
70 int i = 0;
71 uint32_t block_size = mmc_get_device_blocksize();
72
73 index = partition_get_index("mdtp");
74 if (index == 0) {
75 dprintf(CRITICAL, "ERROR: mdtp Partition table not found\n");
76 return 1;
77 }
78
79 ptn = partition_get_offset(index);
80 mmc_set_lun(partition_get_lun(index));
81
82 if (ptn == 0) {
83 dprintf(CRITICAL, "ERROR: mdtp Partition invalid\n");
84 return 1;
85 }
86
87 for(i = 0; i< MAX_PARAMS; i++) {
88 mdtp_img.meta_data.params[i] = -1; //Initiate params for errors check
89 }
90
91 uint8_t *base = memalign(block_size, ROUNDUP(MDTP_HEADER_LEN, block_size));
92 if (!base) {
93 dprintf(CRITICAL, "ERROR: mdtp malloc failed\n");
94 return 1;
95 }
96
97 // read image meta data
98 if (mmc_read(ptn, (void*)base, MDTP_HEADER_LEN)) {
99 dprintf(CRITICAL, "ERROR: mdtp meta data read failed\n");
100 free(base);
101 return 1;
102 }
103
104 uint32_t params_size = MAX_PARAMS * sizeof(uint32_t);
105 uint32_t images_params_size = MAX_IMAGES*sizeof(mdtp_image_params_t);
106 memscpy(mdtp_img.meta_data.params, sizeof(mdtp_img.meta_data.params), base, params_size);
107 memscpy(mdtp_img.meta_data.image_params, META_DATA_PARTITION_LEN,
108 base + sizeof(mdtp_img.meta_data.params), images_params_size);
109
110 dprintf(INFO, "mdtp: mdtp_img loaded\n");
111
112 free(base);
113 return 0;
114}
115