uncompress.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * uncompress.c
  3. *
  4. * Copyright (C) 1999 Linus Torvalds
  5. * Copyright (C) 2000-2002 Transmeta Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License (Version 2) as
  9. * published by the Free Software Foundation.
  10. *
  11. * cramfs interfaces to the uncompression library. There's really just
  12. * three entrypoints:
  13. *
  14. * - cramfs_uncompress_init() - called to initialize the thing.
  15. * - cramfs_uncompress_exit() - tell me when you're done
  16. * - cramfs_uncompress_block() - uncompress a block.
  17. *
  18. * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
  19. * only have one stream, and we'll initialize it only once even if it
  20. * then is used by multiple filesystems.
  21. */
  22. #include <common.h>
  23. #include <malloc.h>
  24. #include <watchdog.h>
  25. #include <u-boot/zlib.h>
  26. static z_stream stream;
  27. /* Returns length of decompressed data. */
  28. int cramfs_uncompress_block (void *dst, void *src, int srclen)
  29. {
  30. int err;
  31. inflateReset (&stream);
  32. stream.next_in = src;
  33. stream.avail_in = srclen;
  34. stream.next_out = dst;
  35. stream.avail_out = 4096 * 2;
  36. err = inflate (&stream, Z_FINISH);
  37. if (err != Z_STREAM_END)
  38. goto err;
  39. return stream.total_out;
  40. err:
  41. /*printf ("Error %d while decompressing!\n", err); */
  42. /*printf ("%p(%d)->%p\n", src, srclen, dst); */
  43. return -1;
  44. }
  45. int cramfs_uncompress_init (void)
  46. {
  47. int err;
  48. stream.zalloc = gzalloc;
  49. stream.zfree = gzfree;
  50. stream.next_in = 0;
  51. stream.avail_in = 0;
  52. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  53. stream.outcb = (cb_func) WATCHDOG_RESET;
  54. #else
  55. stream.outcb = Z_NULL;
  56. #endif /* CONFIG_HW_WATCHDOG */
  57. err = inflateInit (&stream);
  58. if (err != Z_OK) {
  59. printf ("Error: inflateInit2() returned %d\n", err);
  60. return -1;
  61. }
  62. return 0;
  63. }
  64. int cramfs_uncompress_exit (void)
  65. {
  66. inflateEnd (&stream);
  67. return 0;
  68. }