diff mbox series

[7/8] handler: add FreeBSD-compatible copy_write() callback

Message ID 20180423123109.18590-7-christian.storm@siemens.com
State Accepted
Headers show
Series [1/8] core: warn about non-Linux parent SIGTERM tracking | expand

Commit Message

Storm, Christian April 23, 2018, 12:31 p.m. UTC
As FreeBSD likes to have writes to device nodes being
multiples of 512 bytes, provide an according padding
callback function as default for the raw handler.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 handlers/raw_handler.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

Comments

Stefano Babic April 24, 2018, 10:42 a.m. UTC | #1
On 23/04/2018 14:31, Christian Storm wrote:
> As FreeBSD likes to have writes to device nodes being
> multiples of 512 bytes, provide an according padding
> callback function as default for the raw handler.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  handlers/raw_handler.c | 30 +++++++++++++++++++++++++++++-
>  1 file changed, 29 insertions(+), 1 deletion(-)
> 
> diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
> index ac8e47a..e57367e 100644
> --- a/handlers/raw_handler.c
> +++ b/handlers/raw_handler.c
> @@ -19,6 +19,31 @@
>  void raw_handler(void);
>  void raw_filecopy_handler(void);
>  
> +#if defined(__FreeBSD__)
> +/*
> + * FreeBSD likes to have multiples of 512 bytes written
> + * to a device node, hence slice the buffer in palatable
> + * chunks assuming that only the last written buffer's
> + * length is smaller than cpio_utils.c's BUFF_SIZE and
> + * doesn't satisfy length % 512 == 0.
> + */
> +static int copy_write_padded(void *out, const void *buf, unsigned int len)
> +{
> +	if (len % 512 == 0) {
> +		return copy_write(out, buf, len);
> +	}
> +
> +	uint8_t buffer[512] = { 0 };
> +	int chunklen = len - (len % 512);
> +	int res = copy_write(out, buf, chunklen);
> +	if (res != 0) {
> +		return res;
> +	}
> +	memcpy(&buffer, buf+chunklen, len-chunklen);
> +	return copy_write(out, buffer, 512);
> +}
> +#endif
> +
>  static int install_raw_image(struct img_type *img,
>  	void __attribute__ ((__unused__)) *data)
>  {
> @@ -31,8 +56,11 @@ static int install_raw_image(struct img_type *img,
>  				img->device, strerror(errno));
>  		return -1;
>  	}
> -	
> +#if defined(__FreeBSD__)
> +	ret = copyimage(&fdout, img, copy_write_padded);
> +#else
>  	ret = copyimage(&fdout, img, NULL);
> +#endif
>  
>  	close(fdout);
>  	return ret;
> 

Reviewed-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
index ac8e47a..e57367e 100644
--- a/handlers/raw_handler.c
+++ b/handlers/raw_handler.c
@@ -19,6 +19,31 @@ 
 void raw_handler(void);
 void raw_filecopy_handler(void);
 
+#if defined(__FreeBSD__)
+/*
+ * FreeBSD likes to have multiples of 512 bytes written
+ * to a device node, hence slice the buffer in palatable
+ * chunks assuming that only the last written buffer's
+ * length is smaller than cpio_utils.c's BUFF_SIZE and
+ * doesn't satisfy length % 512 == 0.
+ */
+static int copy_write_padded(void *out, const void *buf, unsigned int len)
+{
+	if (len % 512 == 0) {
+		return copy_write(out, buf, len);
+	}
+
+	uint8_t buffer[512] = { 0 };
+	int chunklen = len - (len % 512);
+	int res = copy_write(out, buf, chunklen);
+	if (res != 0) {
+		return res;
+	}
+	memcpy(&buffer, buf+chunklen, len-chunklen);
+	return copy_write(out, buffer, 512);
+}
+#endif
+
 static int install_raw_image(struct img_type *img,
 	void __attribute__ ((__unused__)) *data)
 {
@@ -31,8 +56,11 @@  static int install_raw_image(struct img_type *img,
 				img->device, strerror(errno));
 		return -1;
 	}
-	
+#if defined(__FreeBSD__)
+	ret = copyimage(&fdout, img, copy_write_padded);
+#else
 	ret = copyimage(&fdout, img, NULL);
+#endif
 
 	close(fdout);
 	return ret;