Skip to content

Commit e277026

Browse files
committed
firmware: microchip: move buffer allocation into mpfs_auto_update_set_image_address()
This buffer is used exclusively by mpfs_auto_update_set_image_address(), so move the management of it there, employing the recently added cleanup infrastructure to avoid littering the function with gotos. Signed-off-by: Conor Dooley <[email protected]>
1 parent a2bf9df commit e277026

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

drivers/firmware/microchip/mpfs-auto-update.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*
1010
* Author: Conor Dooley <[email protected]>
1111
*/
12+
#include <linux/cleanup.h>
1213
#include <linux/debugfs.h>
1314
#include <linux/firmware.h>
1415
#include <linux/math.h>
@@ -233,15 +234,17 @@ static int mpfs_auto_update_verify_image(struct fw_upload *fw_uploader)
233234
return ret;
234235
}
235236

236-
static int mpfs_auto_update_set_image_address(struct mpfs_auto_update_priv *priv, char *buffer,
237+
static int mpfs_auto_update_set_image_address(struct mpfs_auto_update_priv *priv,
237238
u32 image_address, loff_t directory_address)
238239
{
239240
struct erase_info erase;
240-
size_t erase_size = AUTO_UPDATE_DIRECTORY_SIZE;
241+
size_t erase_size = round_up(AUTO_UPDATE_DIRECTORY_SIZE, (u64)priv->flash->erasesize);
241242
size_t bytes_written = 0, bytes_read = 0;
243+
char *buffer __free(kfree) = kzalloc(erase_size, GFP_KERNEL);
242244
int ret;
243245

244-
erase_size = round_up(erase_size, (u64)priv->flash->erasesize);
246+
if (!buffer)
247+
return -ENOMEM;
245248

246249
erase.addr = AUTO_UPDATE_DIRECTORY_BASE;
247250
erase.len = erase_size;
@@ -287,7 +290,7 @@ static int mpfs_auto_update_set_image_address(struct mpfs_auto_update_priv *priv
287290
return ret;
288291

289292
if (bytes_written != erase_size)
290-
return ret;
293+
return -EIO;
291294

292295
return 0;
293296
}
@@ -297,7 +300,6 @@ static int mpfs_auto_update_write_bitstream(struct fw_upload *fw_uploader, const
297300
{
298301
struct mpfs_auto_update_priv *priv = fw_uploader->dd_handle;
299302
struct erase_info erase;
300-
char *buffer;
301303
loff_t directory_address = AUTO_UPDATE_UPGRADE_DIRECTORY;
302304
size_t erase_size = AUTO_UPDATE_DIRECTORY_SIZE;
303305
size_t bytes_written = 0;
@@ -313,16 +315,12 @@ static int mpfs_auto_update_write_bitstream(struct fw_upload *fw_uploader, const
313315
image_address = AUTO_UPDATE_BITSTREAM_BASE +
314316
AUTO_UPDATE_UPGRADE_INDEX * priv->size_per_bitstream;
315317

316-
buffer = devm_kzalloc(priv->dev, erase_size, GFP_KERNEL);
317-
if (!buffer)
318-
return -ENOMEM;
319-
320318
/*
321319
* For bitstream info, the descriptor is written to a fixed offset,
322320
* so there is no need to set the image address.
323321
*/
324322
if (!is_info) {
325-
ret = mpfs_auto_update_set_image_address(priv, buffer, image_address, directory_address);
323+
ret = mpfs_auto_update_set_image_address(priv, image_address, directory_address);
326324
if (ret) {
327325
dev_err(priv->dev, "failed to set image address in the SPI directory: %d\n", ret);
328326
return ret;
@@ -345,7 +343,7 @@ static int mpfs_auto_update_write_bitstream(struct fw_upload *fw_uploader, const
345343
dev_info(priv->dev, "Erasing the flash at address (0x%x)\n", image_address);
346344
ret = mtd_erase(priv->flash, &erase);
347345
if (ret)
348-
goto out;
346+
return ret;
349347

350348
/*
351349
* No parsing etc of the bitstream is required. The system controller
@@ -355,19 +353,15 @@ static int mpfs_auto_update_write_bitstream(struct fw_upload *fw_uploader, const
355353
dev_info(priv->dev, "Writing the image to the flash at address (0x%x)\n", image_address);
356354
ret = mtd_write(priv->flash, (loff_t)image_address, size, &bytes_written, data);
357355
if (ret)
358-
goto out;
356+
return ret;
359357

360-
if (bytes_written != size) {
361-
ret = -EIO;
362-
goto out;
363-
}
358+
if (bytes_written != size)
359+
return -EIO;
364360

365361
*written = bytes_written;
366362
dev_info(priv->dev, "Wrote 0x%zx bytes to the flash\n", bytes_written);
367363

368-
out:
369-
devm_kfree(priv->dev, buffer);
370-
return ret;
364+
return 0;
371365
}
372366

373367
static enum fw_upload_err mpfs_auto_update_write(struct fw_upload *fw_uploader, const u8 *data,

0 commit comments

Comments
 (0)