@@ -101,15 +101,16 @@ static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
101
101
int amdgpu_fru_get_product_info (struct amdgpu_device * adev )
102
102
{
103
103
unsigned char buff [34 ];
104
- int addrptr = 0 , size = 0 ;
104
+ int addrptr , size ;
105
+ int len ;
105
106
106
107
if (!is_fru_eeprom_supported (adev ))
107
108
return 0 ;
108
109
109
110
/* If algo exists, it means that the i2c_adapter's initialized */
110
111
if (!adev -> pm .smu_i2c .algo ) {
111
112
DRM_WARN ("Cannot access FRU, EEPROM accessor not initialized" );
112
- return 0 ;
113
+ return - ENODEV ;
113
114
}
114
115
115
116
/* There's a lot of repetition here. This is due to the FRU having
@@ -128,7 +129,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
128
129
size = amdgpu_fru_read_eeprom (adev , addrptr , buff );
129
130
if (size < 1 ) {
130
131
DRM_ERROR ("Failed to read FRU Manufacturer, ret:%d" , size );
131
- return size ;
132
+ return - EINVAL ;
132
133
}
133
134
134
135
/* Increment the addrptr by the size of the field, and 1 due to the
@@ -138,62 +139,65 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
138
139
size = amdgpu_fru_read_eeprom (adev , addrptr , buff );
139
140
if (size < 1 ) {
140
141
DRM_ERROR ("Failed to read FRU product name, ret:%d" , size );
141
- return size ;
142
+ return - EINVAL ;
142
143
}
143
144
145
+ len = size ;
144
146
/* Product name should only be 32 characters. Any more,
145
147
* and something could be wrong. Cap it at 32 to be safe
146
148
*/
147
- if (size > 32 ) {
149
+ if (len >= sizeof ( adev -> product_name ) ) {
148
150
DRM_WARN ("FRU Product Number is larger than 32 characters. This is likely a mistake" );
149
- size = 32 ;
151
+ len = sizeof ( adev -> product_name ) - 1 ;
150
152
}
151
153
/* Start at 2 due to buff using fields 0 and 1 for the address */
152
- memcpy (adev -> product_name , & buff [2 ], size );
153
- adev -> product_name [size ] = '\0' ;
154
+ memcpy (adev -> product_name , & buff [2 ], len );
155
+ adev -> product_name [len ] = '\0' ;
154
156
155
157
addrptr += size + 1 ;
156
158
size = amdgpu_fru_read_eeprom (adev , addrptr , buff );
157
159
if (size < 1 ) {
158
160
DRM_ERROR ("Failed to read FRU product number, ret:%d" , size );
159
- return size ;
161
+ return - EINVAL ;
160
162
}
161
163
164
+ len = size ;
162
165
/* Product number should only be 16 characters. Any more,
163
166
* and something could be wrong. Cap it at 16 to be safe
164
167
*/
165
- if (size > 16 ) {
168
+ if (len >= sizeof ( adev -> product_number ) ) {
166
169
DRM_WARN ("FRU Product Number is larger than 16 characters. This is likely a mistake" );
167
- size = 16 ;
170
+ len = sizeof ( adev -> product_number ) - 1 ;
168
171
}
169
- memcpy (adev -> product_number , & buff [2 ], size );
170
- adev -> product_number [size ] = '\0' ;
172
+ memcpy (adev -> product_number , & buff [2 ], len );
173
+ adev -> product_number [len ] = '\0' ;
171
174
172
175
addrptr += size + 1 ;
173
176
size = amdgpu_fru_read_eeprom (adev , addrptr , buff );
174
177
175
178
if (size < 1 ) {
176
179
DRM_ERROR ("Failed to read FRU product version, ret:%d" , size );
177
- return size ;
180
+ return - EINVAL ;
178
181
}
179
182
180
183
addrptr += size + 1 ;
181
184
size = amdgpu_fru_read_eeprom (adev , addrptr , buff );
182
185
183
186
if (size < 1 ) {
184
187
DRM_ERROR ("Failed to read FRU serial number, ret:%d" , size );
185
- return size ;
188
+ return - EINVAL ;
186
189
}
187
190
191
+ len = size ;
188
192
/* Serial number should only be 16 characters. Any more,
189
193
* and something could be wrong. Cap it at 16 to be safe
190
194
*/
191
- if (size > 16 ) {
195
+ if (len >= sizeof ( adev -> serial ) ) {
192
196
DRM_WARN ("FRU Serial Number is larger than 16 characters. This is likely a mistake" );
193
- size = 16 ;
197
+ len = sizeof ( adev -> serial ) - 1 ;
194
198
}
195
- memcpy (adev -> serial , & buff [2 ], size );
196
- adev -> serial [size ] = '\0' ;
199
+ memcpy (adev -> serial , & buff [2 ], len );
200
+ adev -> serial [len ] = '\0' ;
197
201
198
202
return 0 ;
199
203
}
0 commit comments