Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com> Acked-by: Michael Chan <mchan@broadcom.com> Signed-off-by: David S. Miller <davem@davemloft.net>
···27612761 spin_unlock_bh(&bp->phy_lock);27622762}2763276327642764-/* To be moved to generic lib/ */27652765-static int27662766-bnx2_gunzip(void *gunzip_buf, unsigned sz, u8 *zbuf, int len)27672767-{27682768- struct z_stream_s *strm;27692769- int rc;27702770-27712771- /* gzip header (1f,8b,08... 10 bytes total + possible asciz filename)27722772- * is stripped */27732773-27742774- rc = -ENOMEM;27752775- strm = kmalloc(sizeof(*strm), GFP_KERNEL);27762776- if (strm == NULL)27772777- goto gunzip_nomem2;27782778- strm->workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);27792779- if (strm->workspace == NULL)27802780- goto gunzip_nomem3;27812781-27822782- strm->next_in = zbuf;27832783- strm->avail_in = len;27842784- strm->next_out = gunzip_buf;27852785- strm->avail_out = sz;27862786-27872787- rc = zlib_inflateInit2(strm, -MAX_WBITS);27882788- if (rc == Z_OK) {27892789- rc = zlib_inflate(strm, Z_FINISH);27902790- /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */27912791- if (rc == Z_STREAM_END)27922792- rc = sz - strm->avail_out;27932793- else27942794- rc = -EINVAL;27952795- zlib_inflateEnd(strm);27962796- } else27972797- rc = -EINVAL;27982798-27992799- kfree(strm->workspace);28002800-gunzip_nomem3:28012801- kfree(strm);28022802-gunzip_nomem2:28032803- return rc;28042804-}28052805-28062764static void28072765load_rv2p_fw(struct bnx2 *bp, u32 *rv2p_code, u32 rv2p_code_len,28082766 u32 rv2p_proc)···28162858 text = vmalloc(FW_BUF_SIZE);28172859 if (!text)28182860 return -ENOMEM;28192819- rc = bnx2_gunzip(text, FW_BUF_SIZE, fw->gz_text, fw->gz_text_len);28612861+ rc = zlib_inflate_blob(text, FW_BUF_SIZE, fw->gz_text, fw->gz_text_len);28202862 if (rc < 0) {28212863 vfree(text);28222864 return rc;···28932935 text = vmalloc(FW_BUF_SIZE);28942936 if (!text)28952937 return -ENOMEM;28962896- rc = bnx2_gunzip(text, FW_BUF_SIZE, bnx2_rv2p_proc1, sizeof(bnx2_rv2p_proc1));29382938+ rc = zlib_inflate_blob(text, FW_BUF_SIZE, bnx2_rv2p_proc1, sizeof(bnx2_rv2p_proc1));28972939 if (rc < 0) {28982940 vfree(text);28992941 goto init_cpu_err;29002942 }29012943 load_rv2p_fw(bp, text, rc /* == len */, RV2P_PROC1);2902294429032903- rc = bnx2_gunzip(text, FW_BUF_SIZE, bnx2_rv2p_proc2, sizeof(bnx2_rv2p_proc2));29452945+ rc = zlib_inflate_blob(text, FW_BUF_SIZE, bnx2_rv2p_proc2, sizeof(bnx2_rv2p_proc2));29042946 if (rc < 0) {29052947 vfree(text);29062948 goto init_cpu_err;
+5-1
include/linux/zlib.h
···8282struct internal_state;83838484typedef struct z_stream_s {8585- Byte *next_in; /* next input byte */8585+ const Byte *next_in; /* next input byte */8686 uInt avail_in; /* number of bytes available at next_in */8787 uLong total_in; /* total nb of input bytes read so far */8888···698698#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)699699 struct internal_state {int dummy;}; /* hack for buggy compilers */700700#endif701701+702702+/* Utility function: initialize zlib, unpack binary blob, clean up zlib,703703+ * return len or negative error code. */704704+extern int zlib_inflate_blob(void *dst, unsigned dst_sz, const void *src, unsigned src_sz);701705702706#endif /* _ZLIB_H */
+9-9
lib/zlib_inflate/inffast.c
···6969void inflate_fast(z_streamp strm, unsigned start)7070{7171 struct inflate_state *state;7272- unsigned char *in; /* local strm->next_in */7373- unsigned char *last; /* while in < last, enough input available */7474- unsigned char *out; /* local strm->next_out */7575- unsigned char *beg; /* inflate()'s initial strm->next_out */7676- unsigned char *end; /* while out < end, enough space available */7272+ const unsigned char *in; /* local strm->next_in */7373+ const unsigned char *last; /* while in < last, enough input available */7474+ unsigned char *out; /* local strm->next_out */7575+ unsigned char *beg; /* inflate()'s initial strm->next_out */7676+ unsigned char *end; /* while out < end, enough space available */7777#ifdef INFLATE_STRICT7878 unsigned dmax; /* maximum distance from zlib header */7979#endif8080 unsigned wsize; /* window size or zero if not using window */8181 unsigned whave; /* valid bytes in the window */8282 unsigned write; /* window write index */8383- unsigned char *window; /* allocated sliding window, if wsize != 0 */8383+ unsigned char *window; /* allocated sliding window, if wsize != 0 */8484 unsigned long hold; /* local strm->hold */8585 unsigned bits; /* local strm->bits */8686- code const *lcode; /* local strm->lencode */8787- code const *dcode; /* local strm->distcode */8686+ code const *lcode; /* local strm->lencode */8787+ code const *dcode; /* local strm->distcode */8888 unsigned lmask; /* mask for first level of length codes */8989 unsigned dmask; /* mask for first level of distance codes */9090 code this; /* retrieved table entry */···9292 /* window position, window bytes to copy */9393 unsigned len; /* match length, unused bytes */9494 unsigned dist; /* match distance */9595- unsigned char *from; /* where to copy match from */9595+ unsigned char *from; /* where to copy match from */96969797 /* copy state to local variables */9898 state = (struct inflate_state *)strm->state;
+51-4
lib/zlib_inflate/inflate.c
···332332int zlib_inflate(z_streamp strm, int flush)333333{334334 struct inflate_state *state;335335- unsigned char *next; /* next input */336336- unsigned char *put; /* next output */335335+ const unsigned char *next; /* next input */336336+ unsigned char *put; /* next output */337337 unsigned have, left; /* available input and output */338338 unsigned long hold; /* bit buffer */339339 unsigned bits; /* bits in bit buffer */340340 unsigned in, out; /* save starting available input and output */341341 unsigned copy; /* number of stored or match bytes to copy */342342- unsigned char *from; /* where to copy match bytes from */342342+ unsigned char *from; /* where to copy match bytes from */343343 code this; /* current decoding table entry */344344 code last; /* parent table entry */345345 unsigned len; /* length to copy for repeats, bits to drop */···897897898898 /* Setup some variables to allow misuse of updateWindow */899899 z->avail_out = 0;900900- z->next_out = z->next_in + z->avail_in;900900+ z->next_out = (unsigned char*)z->next_in + z->avail_in;901901902902 zlib_updatewindow(z, z->avail_in);903903···915915 z->avail_in = 0;916916917917 return Z_OK;918918+}919919+920920+#include <linux/errno.h>921921+#include <linux/slab.h>922922+#include <linux/vmalloc.h>923923+924924+/* Utility function: initialize zlib, unpack binary blob, clean up zlib,925925+ * return len or negative error code. */926926+int zlib_inflate_blob(void *gunzip_buf, unsigned sz, const void *buf, unsigned len)927927+{928928+ const u8 *zbuf = buf;929929+ struct z_stream_s *strm;930930+ int rc;931931+932932+ rc = -ENOMEM;933933+ strm = kmalloc(sizeof(*strm), GFP_KERNEL);934934+ if (strm == NULL)935935+ goto gunzip_nomem1;936936+ strm->workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);937937+ if (strm->workspace == NULL)938938+ goto gunzip_nomem2;939939+940940+ /* gzip header (1f,8b,08... 10 bytes total + possible asciz filename)941941+ * expected to be stripped from input */942942+943943+ strm->next_in = zbuf;944944+ strm->avail_in = len;945945+ strm->next_out = gunzip_buf;946946+ strm->avail_out = sz;947947+948948+ rc = zlib_inflateInit2(strm, -MAX_WBITS);949949+ if (rc == Z_OK) {950950+ rc = zlib_inflate(strm, Z_FINISH);951951+ /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */952952+ if (rc == Z_STREAM_END)953953+ rc = sz - strm->avail_out;954954+ else955955+ rc = -EINVAL;956956+ zlib_inflateEnd(strm);957957+ } else958958+ rc = -EINVAL;959959+960960+ kfree(strm->workspace);961961+gunzip_nomem2:962962+ kfree(strm);963963+gunzip_nomem1:964964+ return rc; /* returns Z_OK (0) if successful */918965}