1 /*
2 * xcam_thread.h - xcam basic thread
3 *
4 * Copyright (c) 2014 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21
22 #include "xcam_cpf_reader.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include "libtbd.h"
27
28 #undef XCAM_FAIL_RETURN_VAL
29 #define XCAM_FAIL_RETURN_VAL(exp, ret) \
30 if (!(exp)) { \
31 XCAM_LOG_WARNING ("XCAM_FAIL_RETURN_VAL %s", #exp); \
32 return ret; \
33 }
34
35 #undef XCAM_FAIL_RETURN
36 #define XCAM_FAIL_RETURN(exp) \
37 if (!(exp)) { \
38 XCAM_LOG_WARNING ("XCAM_FAIL_RETURN %s", #exp); \
39 return ; \
40 }
41
xcam_new0(size_t size)42 void *xcam_new0(size_t size)
43 {
44 void *buf = malloc (size);
45 memset (buf, 0, size);
46 return buf;
47 }
48
xcam_cpf_blob_new()49 XCamCpfBlob * xcam_cpf_blob_new ()
50 {
51 return (XCamCpfBlob*) xcam_new0 (sizeof(XCamCpfBlob));
52 }
53
xcam_cpf_blob_free(XCamCpfBlob * blob)54 void xcam_cpf_blob_free (XCamCpfBlob *blob)
55 {
56 XCAM_FAIL_RETURN (blob);
57
58 if (blob->data)
59 xcam_free (blob->data);
60
61 xcam_free (blob);
62 }
63
64 static int32_t
read_cpf_file(const char * cpf_file,uint8_t ** buf)65 read_cpf_file (const char *cpf_file, uint8_t **buf)
66 {
67 int32_t size = 0;
68 FILE *fp = fopen (cpf_file, "rb");
69 XCAM_FAIL_RETURN_VAL (fp, -1);
70
71 *buf = NULL;
72
73 if (fseek (fp, 0, SEEK_END) < 0)
74 goto read_error;
75 if ((size = ftell (fp)) <= 0)
76 goto read_error;
77 if (fseek( fp, 0, SEEK_SET) < 0)
78 goto read_error;
79
80 *buf = (uint8_t*) xcam_new0 (size);
81 XCAM_ASSERT (*buf);
82 if (fread (*buf, 1, size, fp) != (size_t) size)
83 goto read_error;
84
85 fclose (fp);
86 return size;
87
88 read_error:
89 XCAM_LOG_ERROR ("read cpf(%s) failed", cpf_file);
90 fclose (fp);
91 if (*buf) {
92 xcam_free (*buf);
93 *buf = NULL;
94 }
95 return -1;
96
97 }
98
99 boolean
xcam_cpf_read(const char * cpf_file,XCamCpfBlob * aiq_cpf,XCamCpfBlob * hal_cpf)100 xcam_cpf_read (const char *cpf_file, XCamCpfBlob *aiq_cpf, XCamCpfBlob *hal_cpf)
101 {
102 uint8_t *cpf_buf;
103 int32_t cpf_size;
104
105 uint8_t *blob;
106 uint32_t blob_size;
107
108 XCAM_FAIL_RETURN_VAL (cpf_file, FALSE);
109 XCAM_FAIL_RETURN_VAL (aiq_cpf, FALSE);
110
111 /* read cpf */
112 if ((cpf_size = read_cpf_file(cpf_file, &cpf_buf)) <= 0) {
113 XCAM_LOG_ERROR ("read cpf_file(%s) failed.", cpf_file);
114 return FALSE;
115 }
116
117 /* check sum */
118 if (tbd_validate (cpf_buf, cpf_size, tbd_tag_cpff) != tbd_err_none) {
119 XCAM_LOG_ERROR ("tbd validate cpf file(%s) failed.", cpf_file);
120 goto free_buf;
121 }
122
123 /* fetch AIQ */
124 if ( (tbd_get_record (cpf_buf, tbd_class_aiq, tbd_format_any,
125 (void**)&blob, &blob_size) != tbd_err_none) ||
126 !blob || blob_size <= 0) {
127 XCAM_LOG_ERROR ("CPF parse AIQ record failed.");
128 goto free_buf;
129 }
130 aiq_cpf->data = (uint8_t*) malloc (blob_size);
131 XCAM_ASSERT (aiq_cpf->data);
132 aiq_cpf->size = blob_size;
133 memcpy (aiq_cpf->data, blob, blob_size);
134
135
136 #if 0 //DRV not necessary
137 /* fetch DRV */
138 if (tbd_get_record (cpf_buf, tbd_class_drv, tbd_format_any,
139 &drv_blob.data, &drv_blob.size) != tbd_err_none) {
140 XCAM_LOG_ERROR ("CPF parse DRV record failed.");
141 return FALSE;
142 }
143 #endif
144
145
146 /* fetch HAL */
147 if (hal_cpf) {
148 if (tbd_get_record (cpf_buf, tbd_class_hal, tbd_format_any,
149 (void**)&blob, &blob_size) != tbd_err_none) {
150 XCAM_LOG_WARNING ("CPF doesn't have HAL record.");
151 // ignore HAL, not necessary
152 } else if (blob && blob_size > 0) {
153 hal_cpf->data = (uint8_t*) malloc (blob_size);
154 XCAM_ASSERT (hal_cpf->data);
155 hal_cpf->size = blob_size;
156 memcpy (hal_cpf->data, blob, blob_size);
157 }
158 }
159
160 xcam_free (cpf_buf);
161 return TRUE;
162
163 free_buf:
164 xcam_free (cpf_buf);
165 return FALSE;
166
167 }
168